0

可能重复:
LINQ 中的 LEFT OUTER JOIN

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

4

2 回答 2

10

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

您可能会看到:How to: Perform Left Outer Joins (C# Programming Guide) - 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; }
}

左外连接查询可以是:

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) };
于 2013-01-29T05:23:17.833 回答
4
 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 ...);
于 2013-01-29T05:25:37.537 回答