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

时间:2013-03-22 13:04:21

标签: c# .net entity-framework linq-to-entities anonymous-types

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

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

然后我在另一个查询中使用此var:

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')

2 个答案:

答案 0 :(得分:3)

您确定需要加入吗?那怎么样:

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是否可以为空,您可能需要稍微改变一下。

答案 1 :(得分: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 ...