views:

62

answers:

1

I have a very simple linq query that gets data from two datatables (orderHeader & OrderDetails) and joins them together. What I would like to do is take the order items for each order and pass them to a method for processing.

Here is the query:

var results = from orderHeader in dtOrderHeader.AsEnumerable()
join orderDetails in dtOrderDetails.AsEnumerable() on 
  orderHeader.Field<int>("order_ref") equals
  orderDetails.Field<int>("order_id")
select new { 
  orderID = orderHeader.Field<int>("order_ref"), 
  orderItem = orderDetails.Field<string>("product_details") 
};

What is the best way to iterate the results for each order?

Thanks

+1  A: 

This result set contains multiple orders, this would require a nested foreach.

foreach (var order in results.Select(r => r.orderID).Distinct()) {
   Console.WriteLine("Order: " + order);
   Console.WriteLine("Items:");
   foreach (var product in results.Where(r => r.orderItem == order)) {
      Console.WriteLine(product.orderItem);
   }
}
Fabian
You star! Thank you
StuffandBlah
StuffandBlah
Oops, when you select a single value "Select(r => r.orderID)", you get a list of the type you selected. Corrected my answer :)
Fabian
Great! Just needed to add .toString() on the output of the order. Thanks for your help.
StuffandBlah
Just one other question...If I want to be able to pull out more than just the orderID from the orderheader part how can I achieve this?
StuffandBlah