【问题标题】:How to convert SQL inner join to lambda linq query如何将 SQL 内连接转换为 lambda linq 查询
【发布时间】:2017-09-27 12:35:30
【问题描述】:

我是 LINQ 查询的新手 我需要帮助将我的示例 SQL 查询转换为 LINQ lambda 查询

select * from GRecommendations 
inner join GSections
on GRecommendations.GSectionId = GSections.Id
where GSections.GaId = 646

【问题讨论】:

  • 我们不是来为您转换代码的。如果您尝试过但被卡在某个地方,我们非常欢迎您告诉具体您需要帮助的地方。一个明显的建议:不要在 LINQ 中 join,使用导航属性。
  • 是的,先生,你是对的

标签: linq lambda linq-to-sql


【解决方案1】:

当 GRecommendations 是一个集合时,您可以使用两种不同的方法。

var arrResult = //UNTESTED
   GRecommendations
   .Join(GSections.Where(sec => sec.GaId.Equals(646)),
   rec => rec.GeSectionId,
   sec => sec.Id,
   (REC, SEC) => new { /*put here what you want selected*/ }
   );  //

或

var arrResult = 
(
   from rec in GRecommendations
   join rec in GSections.Where(s => s.GaId.Equals(646)) on rec.GSectionId equals sec.GaId
   select new {/*rec.something*/, /*sec.something*/}
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2018-09-23
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多