Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq. How to query list within list?

I have Store List (storeList) object. (Consists of list of Stores) Each Store has list of Addresses. Each address have AddressType property that can be PHYSICAL, ALTERNATIVE or MAILING.

I am trying to return PHYSICAL Address object so I can modify its properties:

This is my first attempt:

StoreAddressList result = 
    (from str in storeList
     where
     str.AssetAddresses.Any(p => p.AddressType.Name == "PHYSICAL")
     select str.AssetAddresses).FirstOrDefault();

As a result I expect to get list with only one item (where address type is PHYSICAL), but I get list with 3 items (with all three types). What's wrong here?

Thanks

like image 515
bobetko Avatar asked Dec 03 '22 06:12

bobetko


1 Answers

Flatten the list of list of addresses by projecting each store to its list of addresses, use SelectMany to achieve the flattening, and then take only those where the address type is physical:

var addressesToModify = 
    storeList.SelectMany(store => store.AssetAddresses)
             .Where(address => address.AddressType.Name == "PHYSICAL");

In query syntax:

var addressesToModify =
    from store in storeList
    from address in store.AssetAddresses
    where address.AddressType.Name == "PHYSICAL"
    select address;

Notice how it reads exactly like we described. From each store in the storeList, from each address in store.AssetAddresses where address is a physical address, select address.

Now iterate the results of the query and modify as needed.

Also, I strongly suggest making an enum

public enum AddressType { Physical, Alternative, Mailing }

and changing Address.AddressType to be of this type

public AddressType AddressType { get; }

and then you can write

where address.AddressType == AddressType.Physical

or

.Where(address => address.AddressType == AddressType.Physical);
like image 111
jason Avatar answered Dec 24 '22 15:12

jason