如何将SQL内部联接转换为lambda linq查询

时间:2017-09-27 12:35:30

标签: linq lambda linq-to-sql

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

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

1 个答案:

答案 0 :(得分:2)

当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*/}
);