views:

45

answers:

1

I am trying to implement outer join on this kind of query for p.Person table. How to do it.

this eg is taken from http://ashishware.com/DSLinqExample.shtml

var onlyinfo = p.Person.Where(n=>n.FirstName.Contains('a')).Join(p.PersonInfo,
                                                                n => n.PersonId,
                                                                m => m.PersonId,
                                                                (n, m) => m).ToArray<Persons.PersonInfoRow>();
+2  A: 

Normally left joins in LINQ are modelled with group joins, sometimes in conjunction with DefaultIfEmpty and SelectMany:

var leftJoin = p.Person.Where(n => n.FirstName.Contains("a"))
                       .GroupJoin(p.PersonInfo, 
                                  n => n.PersonId,
                                  m => m.PersonId,
                                  (n, ms) => new { n, ms.DefaultIfEmpty() })
                       .SelectMany(z => z.ms.Select(m => new { n = z.n, m ));

That will give a sequence of pairs (n, m) where n is the entry from p.Person and m is the entry from p.PersonInfo, but m will be null if there are no matches.

(It's completely untested, btw - but should give you the idea anyway :)

Jon Skeet