Take - Nested

Query
public void Linq21()
{
List customers = GetCustomerList();

var first3WAOrders = (
from c in customers
from o in c.Orders
where c.Region == "WA"
select new { c.CustomerID, o.OrderID, o.OrderDate })
.Take(3);

Console.WriteLine("First 3 orders in WA:");
foreach (var order in first3WAOrders)
{
ObjectDumper.Write(order);
}
}
Lambda Expression
public void Linq21()
{
List customers = GetCustomerList();

var first3WAOrders =
(customers.SelectMany(cust => cust.Orders, (cust, order) => new {cust, order}).
Where(@t => @t.cust.Region == "WA").Select(
@t => new {@t.cust.CustomerID, @t.order.OrderID, @t.order.OrderDate}))
.Take(3);

Console.WriteLine("First 3 orders in WA:");
foreach (var order in first3WAOrders)
{
ObjectDumper.Write(order);
}
}
Output
First 3 orders in WA:
CustomerID=LAZYK OrderID=10482 OrderDate=3/21/1997
CustomerID=LAZYK OrderID=10545 OrderDate=5/22/1997
CustomerID=TRAIH OrderID=10574 OrderDate=6/19/1997
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +