Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

left outer join problem

Tags:

linq

I need to convert some SQL statement to LINQ. How convert LEFT OUTER JOIN to equivalent LINQ statement?

like image 917
Ognjen Avatar asked Apr 30 '10 06:04

Ognjen


1 Answers

You need to use the DefaultIfEmpty operator. The below code should result in a left outer join.

var q = from c in customers
            join o in orders on c.Key equals o.Key into g
            from o in g.DefaultIfEmpty()
            select new {Name = c.Name, OrderNumber = o == null ? "(no orders)" :     o.OrderNumber};

Credit to: http://www.hookedonlinq.com/OuterJoinSample.ashx

like image 83
David Neale Avatar answered Oct 12 '22 03:10

David Neale