C# LINQ 查询中的左外连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14576172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:19:23  来源:igfitidea点击:

Left outer join in LINQ query

c#linq

提问by nsutgio

Possible Duplicate:
LEFT OUTER JOIN in LINQ

可能的重复:
LINQ 中的 LEFT OUTER JOIN

How to make LINQ query with left outer joins?

如何使用左外连接进行 LINQ 查询?

采纳答案by Habib

You can use Enumerable.DefaultIfEmptyMethod for left outer join.

您可以使用Enumerable.DefaultIfEmpty方法进行左外连接。

You may see: How to: Perform Left Outer Joins (C# Programming Guide) - MSDN

您可能会看到:如何:执行左外连接(C# 编程指南)- MSDN

Consider the following example from MSDN,

考虑以下来自 MSDN 的示例,

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class Pet
{
    public string Name { get; set; }
    public Person Owner { get; set; }
}

Left outer join query can be:

左外连接查询可以是:

var query = from person in people
            join pet in pets on person equals pet.Owner into gj
            from subpet in gj.DefaultIfEmpty()
            select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };

回答by Felix

 var query = (from t1 in Context.Table1
              join t2temp in Context.Table2 on t1.Id equals t2.Id into tempJoin
              from t2 in tempJoin.DefaultIfEmpty()
              select ...);