时间:2010-10-19 17:35:53

标签: c# linq outer-join

我有以下查询,但我不知道如何在表1上执行左外连接。

var query = (from r in table1
             join f in table2
                 on r.ID equals f.ID
             select new
             {     
                 r.ID, 
                 r.FirstName,
                 r.LastName,
                 FirstNameOnRecord = 
                     (f != null ? f.FirstName : string.Empty),
                 LastNameOnRecord = 
                     (f != null ? f.LastName : string.Empty),
                 NameChanged = 
                     (f != null 
                         ? (f.FirstName.CompareTo(r.FirstName) == 0 
                             && f.LastName.CompareTo(r.LastName) == 0) 
                         : false)
             }).ToList();

4 个答案:

答案 0 :(得分:5)

以下是left outer join的详细分类。

答案 1 :(得分:5)

答案 2 :(得分:3)

你见过these examples吗?你可能对this part感兴趣的关于Linq的左外连接。

答案 3 :(得分:0)

使用lambda表达式

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