2013-03-22 23 views
6

İki linq sorgum var. Bir sorgunun sonucunu başka bir sorguda kullanmak istiyorum.'Anonymous type' türünde sabit bir değer oluşturulamıyor

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

Sonra başka sorguda bu var kullanmak:

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 } 
      ); 

Ben kendisi tarafından ilk sorgu çalıştırdığınızda bana doğru sonucu verir. Ben join olmadan ikinci birini çalıştırıyorsanız bana doğru sonucu verir ama bir join ile bana aşağıdaki hatayı veriyor:

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

İkinci sorguda 't2s' nerede? –

cevap

3

Are bir katılmaya mı ihtiyacınız var? Bu konuda ne:

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

Sen biraz ProductID ve/veya PROD_ID null olup olmamasına bağlı olarak bir şeyleri değiştirmek gerekebilir.

2

deneyin Sorgularınızı değişen şöyle: Eğer emin

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 ... 
İlgili konular