欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LINQ 学习笔记

程序员文章站 2022-07-03 23:25:12
...

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);