2010-03-08 8 views
5

Perché la seguente query genera l'errore seguente per una riga con un valore NULL per barile quando escludo esplicitamente tali righe nella clausola Where?Filtro 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.

Come dovrebbe la mia domanda/condizione di essere riscritta a lavorare come ho inteso?

risposta

7

Per impostazione predefinita, in set di dati fortemente tipizzati, le proprietà generano tale eccezione se il campo è nullo. È necessario utilizzare il metodo generato 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 il metodo 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

I metodi Is [Field] Null() hanno funzionato perfettamente! Grazie! – Steven

0

Questo ha funzionato per me.

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