selectMany
Demo1:
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 };
等同于:
customers.Where(c => c.Region == "WA")
.SelectMany(c => c.Orders)
.Where(o => o.Orderdate >= cutoffDate)
.Select(x => new { x.OrderID, x.Customer.CustomerID });
Demo2: Category->Products one-many type relationshio
写法1:
var redProducts = context.Category.Single(c => c.Name = "red").Products;
建议写法:
var redProducts = context.Category
.Where(c => c.Name == "red")
.SelectMany(c => c.Products);