0

I have a collection of orders.
Each order has a subcollection of OrderItems. Now I have a Linq query syntax that works:

var ordersProjection = from order in orders
                   from oi in order.OrderItems
                   where oi.Quantity > 1
                   select new { order.OrderID, oi.ProductName, oi.Quantity };

This is quite simple and works just fine. I would like to know the method syntax for this query. I tried hard but don't seem to get it.
I tried with SelectMany but then I loose the condition >1.
Of what I understand every query syntax is translated into method syntax under the hood. So this should be possible.
Thank you.

Andi
  • 57
  • 4
  • Check [C# Language specification](http://www.microsoft.com/en-us/download/details.aspx?id=7029) section 7.16.2 Query expressions translations – Lukasz Szozda Aug 16 '15 at 18:41
  • Not tested but should be similar to this `orders.SelectMany(order=> order.OrderItems.Select(oi => new { order.OrderID, oi.ProductName, oi.Quantity })) .Where(x=> x.Quantity>1)` – Eser Aug 16 '15 at 18:45
  • 1
    Thank you Eser - it works. As I understand it, after SelectMany you create first the flat list. "New" flattens the list so that every variable has an equal standing. Then a random parameter can refer to a variable in the list by specifying the correct "flattened" name after attaching a where clause on the flattened list thru the Dot-operator. Besides, this is not a duplicate as I looked around and did not find a "from collection from subcollection" anywhere. In LinqPad I did not find the button or menu entry to translate from query to method syntax either. Thanks to Eser it is solved. – Andi Aug 16 '15 at 20:11
  • This works too, by the way: var projectionResult=orders.SelectMany(order=> order.OrderItems, (o,oi) => new { o.OrderID, oi.ProductName, oi.Quantity }) .Where(x=> x.Quantity>1); – Andi Aug 16 '15 at 20:21

0 Answers0