LEFT OUTER JOIN in LINQ

后端 未结 22 2461
臣服心动
臣服心动 2020-11-21 04:49

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Corr

22条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 05:25

    Using lambda expression

    db.Categories    
      .GroupJoin(db.Products,
          Category => Category.CategoryId,
          Product => Product.CategoryId,
          (x, y) => new { Category = x, Products = y })
      .SelectMany(
          xy => xy.Products.DefaultIfEmpty(),
          (x, y) => new { Category = x.Category, Product = y })
      .Select(s => new
      {
          CategoryName = s.Category.Name,     
          ProductName = s.Product.Name   
      });
    

提交回复
热议问题