2013-03-22 248 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 ... 
相關問題