2012-07-24 103 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