无法创建类型为“匿名类型”的常量值

7

我有两个linq查询,我想在一个查询中使用另一个查询的结果。

var t2s = (from temp3 in _ent.Products                       
           where temp3.Row_Num == 2
           select new { temp3.ProductID });

然后我在另一个查询中使用这个变量:

var _query = (from P1 in _ent.brands
              join temp2 in on 
                  new { Produ_ID = (Int32?)P1.Prod_ID } 
                  equals new { Produ_ID = (Int32?)temp2.ProductID }
             );

当我单独运行第一个查询时,它会给我正确的结果。如果我不使用join运行第二个查询,它也会给我正确的结果,但是使用join运行时会出现以下错误:

错误:无法创建类型为“匿名类型”的常量值。只有原始类型(如Int32、String和Guid)在此上下文中得到支持。


4
第二个查询中的t2s在哪里? - Denys Denysenko
2个回答

3

您确定需要使用join吗?尝试使用以下方式:

var t2s = _ent.Products.Where(t => t.Row_Num == 1).Select(t =>t.ProductID);

var _query = _ent.brands.Where(b => t2s.Contains(b.Prod_ID));

根据ProductID和/或Prod_ID是否可为空,您可能需要稍微更改一些内容。


2
尝试按以下方式更改您的查询:
var t2s = from temp3 in _ent.Products                       
          where
             temp3.Row_Num == 2
          select temp3.ProductID;

var _query = from P1 in _ent.brands
             join temp2 in t2s on P1.Prod_ID equals temp2 
             select ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接