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.

4
  • Check C# Language specification section 7.16.2 Query expressions translations Commented Aug 16, 2015 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) Commented Aug 16, 2015 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. Commented Aug 16, 2015 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); Commented Aug 16, 2015 at 20:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.