Linq SelectMany request

I have the following query:

DateTime cutoffDate = new DateTime(1997, 1, 1); var orders = from c in customers where c.Region == "WA" from o in c.Orders where o.OrderDate >= cutoffDate select new { c.CustomerID, o.OrderID }; 

How can this be written to Linq Lambda? BTW, is this known as a SelectMany request?

It can also be done by connecting, what are the pros and cons with this, as shown above.

+4
source share
1 answer

Yes, this is SelectMany . You use SelectMany to "smooth" a nested or multi-level collection (in this case, orders are nested under customers) in a simple single-level collection.

 customers.Where(c => c.Region == "WA") .SelectMany(c => c.Orders) .Where(o => o.Orderdate >= cutoffDate) .Select(x => new { x.OrderID, x.Customer.CustomerID }); 

If Orders are a property of the Customer, then there is no need to use a connection.

+7
source

Source: https://habr.com/ru/post/1433182/


All Articles