2012-07-24 157 views
0

我正在使用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 

回答

0

我得到錯誤,因爲我使用IIF進行空檢查。

有時它返回null和查詢被扔空引用異常。

現在我使用的IF代替IIF和它工作正常。

  • 納雷什
0

在選擇後使用new來創建匿名類型並將其括在大括號中。

customOrderLines = (From o In orderLines 
        Select new { 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}) 

我希望這會有所幫助。

+0

感謝它也沒有給予任何的結果reply.But。 – Naresh 2012-07-24 08:23:27

0

您正在收到的錯誤表明您嘗試運行的LINQ未返回任何值,因此customOrderLines將作爲空集合返回。由於LINQ包括相當多的過濾器,我建議刪除Dim customOrderLines As Object,而是做這樣的事情:

Dim 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) 

if customOrderLines isnot nothing andalso customOrderLines.Any then 
    results.DataSource = customOrderLines 
    results.DataBind() 
    results.Visible = True 
    btnExportButton.Visible = True 
else 
    results.DataSource = nothing 
    results.DataBind() 
    results.Visible = False 
    btnExportButton.Visible = False 
end if 

這將確保您不嘗試綁定一個空的集合,也將清除出你的datagrid如果沒有由LINQ返回。

但主要問題仍然是你的LINQ沿線某處返回一個空的集合。我主要建議回去,並確保LINQ實際上帶回了您期望使用LINQpad或其他類似的實用程序。

你可以得到LinqPad Here