2010-03-08 22 views
5

Tại sao truy vấn sau đây làm tăng lỗi dưới đây cho một hàng có giá trị NULL cho thùng khi tôi lọc một cách rõ ràng các hàng đó trong mệnh đề Where?Lọc DBNull Với 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.

Làm thế nào truy vấn của tôi/điều kiện nên được viết lại để làm việc như tôi dự định?

Trả lời

7

Theo mặc định, trong bộ dữ liệu được nhập mạnh, thuộc tính sẽ ném ngoại lệ đó nếu trường đó là rỗng. Bạn cần phải sử dụng tạo ra Is[Field]Null phương pháp:

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) 

Hoặc phương pháp 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

Phương thức [Field] Null() hoàn toàn hoạt động! Cảm ơn! – Steven

0

này đã làm việc cho tôi.

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