测试LINQ没有返回

din*_*tom 1 .net linq vb.net asp.net

使用LINQ to Entities时如何测试对象没有结果?例如,如果custid没有订单历史记录,则以下查询将不返回任何结果但是页面仍将加载,它只是没有gridview,因为它没有要绑定的数据源.

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}
Run Code Online (Sandbox Code Playgroud)

你如何测试对象没有结果?如果没有返回结果,我想测试它为页面提供不同的标记.显然我已经尝试过如果result = vbNull,也尝试过Nothing,那些都行不通.另一个问题是在Try Catch块中使用此查询时,即使没有返回结果也没有捕获异常,我猜它不会将其视为错误.

Mah*_*mal 5

您可以这样使用Any():

Dim hasElements As Boolean = result.Any();
Run Code Online (Sandbox Code Playgroud)