2013-03-22 603 views
6

我有两个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使我有以下错误:

error: Unable to create a constant value of type 'Anonymous type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context

+4

第二个查询中的't2s'在哪里? – 2013-04-10 19:54:19

回答

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是否可以为空。

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 ... 
相关问题