0

Possible Duplicate:
LEFT OUTER JOIN in LINQ

How to make LINQ query with left outer joins?

Community
  • 1
  • 1
nsutgio
  • 240
  • 2
  • 6
  • 15
  • 1
    Just type *c# linq left outer join* in google and get at least [How to: Perform Left Outer Joins (C# Programming Guide)](http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx) – horgh Jan 29 '13 at 05:29
  • 1
    @KonstantinVasilcov Now, now, Konsty - be nice to new commers. You should give him an appropriate link [like this one](http://bit.ly/WuihEC). Don't you agree? – Konrad Viltersten Jan 29 '13 at 07:29
  • @KonradViltersten 81 of reputation is fair enough to be already aware of the rules of this site. I guess I had given him all I was expected to. – horgh Jan 29 '13 at 07:41
  • @KonstantinVasilcov I suspect you missed my point. Click the link and see what I meant. :) – Konrad Viltersten Jan 29 '13 at 08:03
  • Related post - [Linq join iquery, how to use defaultifempty](https://stackoverflow.com/q/19293844/465053) – RBT Mar 07 '19 at 05:26

2 Answers2

10

You can use Enumerable.DefaultIfEmpty Method for left outer join.

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

Consider the following example from 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) };
Habib
  • 219,104
  • 29
  • 407
  • 436
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 ...);
Felix
  • 1,205
  • 9
  • 17