使用VB.net中的linq搜索对象列表中的对象列表

时间:2010-12-22 17:48:37

标签: .net vb.net linq lambda

我有2个班级

Public Class Shipper

    Public Property ShipperID As Long
    Public Property CreateDate As DateTime
    Public Property ShipperCode As String
    Public Property ShipperName As String
    Public Property ShippingMethods As List(Of ShippingMethod)

Public Class ShippingMethod

    Public Property ShippingMethodID As Long
    Public Property ShipperID As Long
    Public Property CreateDate As DateTime
    Public Property ShipppingMethod As String
    Public Property ShipppingMethodCode As String

我正在尝试搜索托运人名单为空白且托运人姓名不为空白的托运人名单 - 我是通过

获得的
Dim oListShipper As List(Of Shipper) = GetAllShippers()
Dim oListShipperRet As List(Of Shipper) = _
        oListShipper.FindAll(Function(c) (c.ShipperCode = "" And _
                                          c.ShipperName <> ""))

现在如何让所有托运人获得ShippingMethod.ShipppingMethodCode =''和ShippingMethod.ShipppingMethod&lt;&gt;''

我试过

oListShipper.FindAll(Function(c) (c.ShipperCode = "" And _
                                  c.ShipperName <> "")) Or _
(c.ShippingMethods.FindAll(Function(d) d.ShipppingMethodCode = "" And _
                                       d.ShipppingMethod <> "").Count > 1)))

但没有奏效。有什么想法吗?

由于 Jothish

1 个答案:

答案 0 :(得分:5)

我相信FindAll方法直接附加到List类,并不是LINQ的真正组成部分。请尝试使用Where和Any方法。我的VB技能很生疏,但我认为它是这样的:

Dim oListShipper As List(Of Shipper) = GetAllShippers()
Dim oListShipperRet As List(Of Shipper) = oListShipper
    .Where(Function(c) (c.ShipperCode = "" And c.ShipperName <> "")
        OR (c.ShippingMethods.Any(
            Function(d) d.ShipppingMethodCode = "" And d.ShipppingMethod <> "")
    ))
    .ToList();