0

Sql:

select a.id, b.name
from a
left join b on a.id = b.id

I want to use lambda in EF to get the same result in this sql

Here is what I've done:

var list = entities.a
    .GroupJoin(
        entities.b, 
        a => a.id, 
        b => b.id, 
        (a, b) => new { a, b })
    .Select(o => o)
    .ToList();

Here Select(o => o), I just don't know how to get the same result in sql

select a.id, b.name
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
wtf512
  • 4,487
  • 9
  • 33
  • 55

1 Answers1

0

I think this article from MSDN would be a lot helpful...

http://msdn.microsoft.com/en-us/library/bb397895.aspx

This is a quote from above article, that might help...

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) };

UPDATE:

As per your request, the Lambda Expression would be something like this...

.SelectMany(@a => @a.@a.b.DefaultIfEmpty(), (@a, joineda) => new {@a, joineda})

Not sure, if it is correct, but it is a starting point, at least...

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
  • Thanks. But I want to use lambda expresssion. I don't know how to code SELECT part `.select()` – wtf512 Sep 03 '14 at 05:17
  • The `DefaultIfEmpty()` should be the key here. Please check where you can use that in the Lambda Expression. Is there any specific reason you want to use Lambda Expression? The linq query works perfectly... – Naveed Butt Sep 03 '14 at 06:08
  • Linq is fine, I konw. I just want to have another way to get what I want :) – wtf512 Sep 03 '14 at 06:25