views:

46

answers:

3

since DefaultIfEmpty() is not supported, what is the proper way of doing a left join?

A: 

Hard to say how to solve your problem, since you don't say what it is, but generally:

var q = from i in Context.Invoices
        select new 
        {
            Number = i.Number,
            ItemNumbers = from il in i.Lines
                          select il.Number
        }

LINQ to Entities will coalesce nulls, so you get an empty collection if there are no invoice line items.

Now this produces a graph, rather than a tabular result set. My wild guess is that's what you want, since when you work in LINQ to Entities you generally want to work with objects, rather than tabular SQL results. But like you said, it's hard to be more specific without knowing the precise problem you're trying to solve.

Craig Stuntz
when I put in many field from the same table, the end result,(sql query) use a lot of unionall stuff
Fredou
I can't suggest a good answer based on so little information.
Craig Stuntz
A: 

in the end, I used the let way with .FirstOrDefault()

from myothertable
where....
let var = (query).FirstOrDefault()
select new {otherfield, var.field1, var.field2}
Fredou
A: 

Fredou,

I have been searching for how to do a Left Join in L2E for several months now. I thought that it could not be done. Please send me a full query showing how this is done. The last query that you sent did not explain clear enough for me to understand! Thanks!

Kyle
look at the link in my answer, there is a working example
Fredou
see this examplehttp://stackoverflow.com/questions/2772017/linq-to-entities-left-join/2772550#2772550
chugh97