spdro
spdro

Reputation: 520

linq to sql join on multiple columns using lambda

Can someone help me to translate this

var query = from s in context.ShoppingMalls
join h in context.Houses
on
new { s.CouncilCode, s.PostCode }
equals
 new { h.CouncilCode, h.PostCode }
select s;

into lambda query?

Thanks.

Upvotes: 33

Views: 58708

Answers (2)

JoeCo
JoeCo

Reputation: 705

Although the example and answer given by @Thomas Levesque works for columns that match, I wanted to also supply the answer if you have columns to join on but they have different names. This is what I needed for my googling and this question got me close.

The difference of course is the explicit declaration of the columns as a variable to identify on.

var query = context.MapKitsToResources
              .Join(
                     context.Resources, 
                     o => new { Id = o.ResourceId, Type = o.ResourceTypeId},
                     i => new { Id = i.Id, Type = TypeId},
                     (o, i) = new { rType : i };

Upvotes: 23

Thomas Levesque
Thomas Levesque

Reputation: 292345

var query = context.ShoppingMalls
                   .Join(
                       context.Houses,
                       s => new { s.CouncilCode, s.PostCode },
                       h => new { h.CouncilCode, h.PostCode },
                       (s, h) => s);

Upvotes: 61

Related Questions