我正在使用LINQ查詢來填充數據網格。作爲新增強的一部分,我必須通過在WHERE子句中包含更多條件來更改現有的LINQ查詢。 看着這麼多帖子,感覺堆放WHERE子句的條件會很簡單。 早些時候查詢返回簡單的對象類型(匿名)並正常工作。 現在我將查詢分成兩部分。在第一部分中,我試圖返回已知類型並試圖在下一部分中的where子句中進行堆疊。 但有些如何這不工作,並沒有取得任何結果。它在grid.DataBind上拋出NULL引用異常拋出異常(空引用異常)。 在這裏發佈我的代碼。LINQ查詢拋出空引用異常
Using db As New ProjectDataContext
Dim orderLines As IEnumerable(Of orderline)
Dim customOrderLines As Object
Try
If VATSearch = 1 Then
' Show only VAT orders
'orderlines = (From O In db.orderlines Where O.order.order_date > MinTime And O.order.order_date < MaxTime And O.order.order_status_fk > 1 And (O.ol_vat_free Is Nothing OrElse O.ol_vat_free = 0) Order By O.order.order_date _
' Select OrderLineID = O.ol_id, Ref = O.order.order_ref, Email = CStr(IIf(O.order.User Is Nothing, O.order.order_billing_email, O.order.User.user_email)), Code = O.StockItem.productsbycolor.product.product_code & O.StockItem.productsbycolor.color.color_code, Size = O.size.size_code, Qty = O.ol_qty, Price = O.ol_product_price, LineTotal = O.ol_lineprice, Delivery = (O.order.order_delivery_total/O.order.orderlines.Count), NonVAT = O.order.order_vat_free _
' )
orderLines = (From o In db.orderlines Where o.order.order_date > MinTime And o.order.order_date < MaxTime And o.order.order_status_fk > 1 And (o.ol_vat_free Is Nothing OrElse o.ol_vat_free = 0) Order By o.order.order_date _
Select o)
ElseIf VATSearch = 2 Then
' Show only non-VAT orders
'orderlines = (From O In db.orderlines Where O.order.order_date > MinTime And O.order.order_date < MaxTime And O.order.order_status_fk > 1 And (Not O.ol_vat_free Is Nothing) AndAlso O.ol_vat_free = 1 Order By O.order.order_date _
' Select OrderLineID = O.ol_id, Ref = O.order.order_ref, Email = CStr(IIf(O.order.User Is Nothing, O.order.order_billing_email, O.order.User.user_email)), Code = O.StockItem.productsbycolor.product.product_code & O.StockItem.productsbycolor.color.color_code, Size = O.size.size_code, Qty = O.ol_qty, Price = O.ol_product_price, LineTotal = O.ol_lineprice, Delivery = (O.order.order_delivery_total/O.order.orderlines.Count), NonVAT = O.order.order_vat_free _
' )
orderLines = (From O In db.orderlines Where O.order.order_date > MinTime And O.order.order_date < MaxTime And O.order.order_status_fk > 1 And (Not O.ol_vat_free Is Nothing) AndAlso O.ol_vat_free = 1 Order By O.order.order_date _
Select O)
Else
' Show both VAT and non-VAT orders
'orderlines = (From O In db.orderlines Where O.order.order_date > MinTime And O.order.order_date < MaxTime And O.order.order_status_fk > 1 Order By O.order.order_date _
' Select OrderLineID = O.ol_id, Ref = O.order.order_ref, Email = CStr(IIf(O.order.User Is Nothing, O.order.order_billing_email, O.order.User.user_email)), Code = O.StockItem.productsbycolor.product.product_code & O.StockItem.productsbycolor.color.color_code, Size = O.size.size_code, Qty = O.ol_qty, Price = O.ol_product_price, LineTotal = O.ol_lineprice, Delivery = (O.order.order_delivery_total/O.order.orderlines.Count), NonVAT = O.order.order_vat_free _
' )
orderLines = (From o In db.orderlines Where o.order.order_date > MinTime And o.order.order_date < MaxTime And o.order.order_status_fk > 1 Order By o.order.order_date _
Select o)
End If
customOrderLines = (From o In orderLines
Select orderLineID = o.ol_id, ref = o.order.order_ref, email = CStr(IIf(o.order.User Is Nothing, o.order.order_billing_email, o.order.User.user_email)),
code = o.StockItem.productsbycolor.product.product_code & o.StockItem.productsbycolor.color.color_code,
size = o.size.size_code, qty = o.ol_qty, price = o.ol_product_price, lineTotal = o.ol_lineprice,
delivery = (o.order.order_delivery_total/o.order.orderlines.Count), nonVAT = o.order.order_vat_free, orderPaymentType = o.order.order_google_order_number)
results.DataSource = customOrderLines
results.DataBind()
results.Visible = True
btnExportButton.Visible = True
Catch ex As Exception
End Try
End Using
感謝它也沒有給予任何的結果reply.But。 – Naresh 2012-07-24 08:23:27