linq to sql join on multiple columns using lambda

2020-05-21 12:35发布

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.

标签: c# sql linq lambda
2条回答
何必那么认真
2楼-- · 2020-05-21 12:38

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 };
查看更多
走好不送
3楼-- · 2020-05-21 12:51
var query = context.ShoppingMalls
                   .Join(
                       context.Houses,
                       s => new { s.CouncilCode, s.PostCode },
                       h => new { h.CouncilCode, h.PostCode },
                       (s, h) => s);
查看更多
登录 后发表回答