2011-05-24 12 views
11

he escrito debajo de consulta en LINQ para realizar izquierda unirse pero su error de lanzamiento:error en LINQ combinación izquierda

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() 
      join p in dc.product_category_feature_trans_SelectAll() 
      on c.cft_id equals p.cft_id into cp 
      from p in cp.DefaultIfEmpty()      
      select new 
      { 
       c.cft_id, 
       c.feature_id, 
       c.feature_name, 
       p.product_id , 
       p.value 
      }; 

error:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information about 
the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 

Line 57:      on c.cft_id equals p.cft_id into cp 
Line 58:      from p in cp.DefaultIfEmpty()      
error Line 59:      select new 
Line 60:      { 
Line 61:       c.cft_id, 

favor me ayude.

Respuesta

13

cp.DefaultIfEmpty() devuelve una secuencia que tendrá un único valor nulo en si cp estaba vacío.

Eso significa que hay que tener en cuenta el hecho de que el p en

from p in cp.DefaultIfEmpty() 

puede ser nulo. Ahora, realmente no ha dicho lo que quiere que suceda en ese caso. Es posible que desee algo como esto:

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() 
      join p in dc.product_category_feature_trans_SelectAll() 
      on c.cft_id equals p.cft_id into cp 
      from p in cp.DefaultIfEmpty()      
      select new 
      { 
       c.cft_id, 
       c.feature_id, 
       c.feature_name, 
       product_id = p == null ? null : p.product_id, 
       value = p == null ? null : p.value 
      }; 

... o es posible que desee un manejo diferente. No conocemos los tipos de p.product_id o p.value, lo que no ayuda. (Por ejemplo, necesitará un poco más de trabajo con el código anterior si product_id es un tipo de valor.)

+0

'product_id = p == null? null: p.product_id' no compilará, si product_id es int –

+1

@Alex: Por eso escribí la última oración ... su versión no compilará si 'product_id' es una cadena :) –

+1

no hay error ... como de costumbre, Jon siempre viene con la respuesta correcta con la explicación adecuada. Gracias Jon. –

9

Usted está haciendo la combinación izquierda, por lo que p puede ser null. Tienes que dar cuenta de eso.

Aquí está la consulta que debería funcionar, aunque no estoy seguro de qué tipo de valor es p.value. La consulta funcionará si el valor es un tipo de referencia. Si el valor es tipo de valor, entonces use el modelo similar al elenco product_id.

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active() 
      join p in dc.product_category_feature_trans_SelectAll() 
      on c.cft_id equals p.cft_id into cp 
      from p in cp.DefaultIfEmpty() 
      select new 
      { 
       c.cft_id, 
       c.feature_id, 
       c.feature_name, 
       product_id = p == null ? (int?)null : p.product_id, 
       value = p == null ? null : p.value, 
      }; 
+0

Gracias funcionó para mí .. –

0

Use su clase de modelo como parámetro para la función DefaultIfEmpty().

from p in cp.DefaultIfEmpty(new yourModelClass()) 
Cuestiones relacionadas