1

When using LINQ to Entities how do you test for the object to have no result? For example, the following query will return no result if the custid has no order history BUT the page will still load, it just won't have a gridview because there is no datasource for it to bind to.

Dim result = From c In ctx.orders
                 Where c.CustomerId = custId
          Join prod In ctx.products On prod.Id Equals c.ProductId
        Select New With
        {c.OrderDate,
         c.PurchaseOrderNumber,
         prod.description,
         c.ProductPrice,
         c.ProductQty}

How do you test the object for no result? I want to test for that to supply different markup to the page if no result is returned. Obviously I've tried If result = vbNull, also tried Is Nothing, those don't work. The other problem is when using this query in a Try Catch block, even when no result is returned it does not catch an exception, I'm guessing it doesn't see that as an error.

4

3 回答 3

5

你可以Any()这样使用:

Dim hasElements As Boolean = result.Any();
于 2012-11-17T14:41:36.083 回答
2

绑定数据后,您可以检查gridview中的行数;不会AnyToList.

于 2012-11-17T15:06:08.473 回答
1

如果你result稍后使用,那么最好将它转换为第List<T>一个,这样它就不会多次连接到数据库

暗淡列表 = result.ToList()

然后,因为它已经被回答,你可以打电话list.Any()

于 2012-11-17T14:54:39.163 回答