简体   繁体   中英

Searching a List of objects in an object list using linq in VB.net

I have 2 classes

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

I am trying to search for list of shippers where shippercode is blank and ShipperName not blank - I got that by

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

Now how do I get all shippers where ShippingMethod.ShipppingMethodCode ='' and ShippingMethod.ShipppingMethod <>''

I tried

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

But didn't work. Any idea ?

Thanks Jothish

I believe the FindAll method is attached directly to the List class, and isn't really part of LINQ. Try using the Where and Any methods instead. My VB skills are rusty, but I think it'd be something like this:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM