views:

180

answers:

1

Hi, I have 3 classes

public class Item { ... } public class Order { public List Items ... } public class Customer { public List Orders ... }

Now, using LINQ I need to get all items that a customer bought. How can I?

I tried something like var items = from o in cust.Orders select o.Items; but result is IEnuberable> and I wanna just one IEnumerable.

I'm asking here to try to avoid coding 2 loops.

+11  A: 

You need SelectMany, which is represented in query expressions as second (and subsequent) from clauses:

var items = from order in customer.Orders
            from item in order.Items
            select item;

Alternatively, to ignore query expressions:

var items = customer.Orders.SelectMany(order => order.Items);
Jon Skeet
SelectMany is nice! :-)
Rune Grimstad