2010-03-08 14 views
5

¿Por qué la siguiente consulta plantea el siguiente error para una fila con un valor NULL para barril cuando excluyo explícitamente esas filas en la cláusula Where?Filtrando DBNull con LINQ

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _ 
      Where Not IsDBNull(row.Cal) AndAlso tiCal_drop.Text = row.Cal _ 
      AndAlso Not IsDBNull(row.Tran) AndAlso tiTrans_drop.Text = row.Tran _ 
      AndAlso Not IsDBNull(row.barrel) _ 
      Select row.barrel 
If query.Count() > 0 Then tiBarrel_txt.Text = query(0) 

Run-time exception thrown : System.Data.StrongTypingException - The value for column 'barrel' in table 'conformal' is DBNull.

¿Cómo se deben reescribir la pregunta/condiciones para que funcione como era mi intención?

Respuesta

7

De forma predeterminada, en datasets fuertemente tipados, las propiedades generan esa excepción si el campo es nulo. Es necesario utilizar el método generada Is[Field]Null:

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _ 
      Where Not row.IsCalNull() AndAlso tiCal_drop.Text = row.Cal _ 
      AndAlso Not row.IsTranNull() AndAlso tiTrans_drop.Text = row.Tran _ 
      AndAlso Not row.IsbarrelNull() _ 
      Select row.barrel 
If query.Count() > 0 Then tiBarrel_txt.Text = query(0) 

o el método DataRow.IsNull:

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _ 
      Where Not row.IsNull("Cal") AndAlso tiCal_drop.Text = row.Cal _ 
      AndAlso Not row.IsNull("Tran") AndAlso tiTrans_drop.Text = row.Tran _ 
      AndAlso Not row.IsNull("barrel") _ 
      Select row.barrel 
If query.Count() > 0 Then tiBarrel_txt.Text = query(0) 
+1

The Is [Field] Null() ¡Los métodos funcionaron perfectamente! ¡Gracias! – Steven

0

Esto funcionó para mí.

Dim query = From row As dbDataSet.conformalRow 
     In dbDataSet.Tables("conformal") _ 
     Where row.Cal.Length > 0 AndAlso tiCal_drop.Text = row.Cal _ 
      AndAlso row.Tran.Length > 0 AndAlso tiTrans_drop.Text = row.Tran _ 
      AndAlso row.barrel.Length > 0 _ 
     Select row.barrel 
Cuestiones relacionadas