C# 使用 lambda 在多列上连接 linq 到 sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10317117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:20:35  来源:igfitidea点击:

linq to sql join on multiple columns using lambda

c#sqllinqlambda

提问by spdro

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?

进入 lambda 查询?

Thanks.

谢谢。

回答by Thomas Levesque

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

回答by JoeCo

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.

尽管@Thomas Levesque 给出的示例和答案适用于匹配的列,但如果您有要加入的列但名称不同,我也想提供答案。这就是我使用谷歌搜索所需要的,这个问题让我很接近。

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