2013-03-22 31 views
6

Ho due query linq. Voglio utilizzare il risultato di una query in un'altra query.Impossibile creare un valore costante di tipo "Tipo anonimo"

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

allora io uso questo var in un'altra domanda:

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

Quando eseguo la prima query di per sé mi dà il risultato giusto. Se corro il secondo senza un join mi dà risultato giusto, ma con un join mi dà il seguente errore:

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

Dov'è il 't2s' nella seconda query? –

risposta

3

Sei certo hai bisogno di unirti? Che dire di questo:

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

Potrebbe essere necessario cambiare le cose un po 'a seconda che ProductID e/o prod_id sono annullabili.

2

prova a modificare le query come segue:

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 ... 
Problemi correlati