Query
public void Linq19()
{
List customers = GetCustomerList();
var customerOrders =
customers.SelectMany(
(cust, custIndex) =>
cust.Orders.Select(o => "Customer #" + (custIndex + 1) +
" has an order with OrderID " + o.OrderID));
ObjectDumper.Write(customerOrders);
}
Lambda Expression
public void DataSetLinq19()
{
var customers = testDS.Tables["Customers"].AsEnumerable();
var orders = testDS.Tables["Orders"].AsEnumerable();
var customerOrders =
customers.SelectMany(
(cust, custIndex) =>
orders.Where(o => cust.Field("CustomerID") == o.Field("CustomerID"))
.Select(o => new { CustomerIndex = custIndex + 1, OrderID = o.Field("OrderID") }));
foreach (var c in customerOrders)
{
Console.WriteLine("Customer Index: " + c.CustomerIndex +
" has an order with OrderID " + c.OrderID);
}
}
Output
Customer #1 has an order with OrderID 10643
Customer #1 has an order with OrderID 10692
Customer #1 has an order with OrderID 10702
Customer #1 has an order with OrderID 10835
Customer #1 has an order with OrderID 10952
Customer #1 has an order with OrderID 11011
Customer #2 has an order with OrderID 10308
Customer #2 has an order with OrderID 10625
Customer #2 has an order with OrderID 10759
Customer #2 has an order with OrderID 10926
...
Customer #90 has an order with OrderID 10248
Customer #90 has an order with OrderID 10615
Customer #90 has an order with OrderID 10673
Customer #90 has an order with OrderID 10695
Customer #90 has an order with OrderID 10873
Customer #90 has an order with OrderID 10879
Customer #90 has an order with OrderID 10910
Customer #90 has an order with OrderID 11005
Customer #91 has an order with OrderID 10374
Customer #91 has an order with OrderID 10611
Customer #91 has an order with OrderID 10792
Customer #91 has an order with OrderID 10870
Customer #91 has an order with OrderID 10906
Customer #91 has an order with OrderID 10998
Customer #91 has an order with OrderID 11044