selectMany html
Demo1:code
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);