Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq to sql join on multiple columns using lambda

Tags:

c#

sql

lambda

linq

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.

like image 289
spdro Avatar asked Apr 25 '12 13:04

spdro


2 Answers

var query = context.ShoppingMalls
                   .Join(
                       context.Houses,
                       s => new { s.CouncilCode, s.PostCode },
                       h => new { h.CouncilCode, h.PostCode },
                       (s, h) => s);
like image 162
Thomas Levesque Avatar answered Sep 25 '22 04:09

Thomas Levesque


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 };
like image 37
JoeCo Avatar answered Sep 23 '22 04:09

JoeCo