如何将此GROUP BY / MIN SQL查询转换为LINQ?

时间:2009-03-17 13:36:35

标签: sql linq linq-to-sql translate

我计划在子查询中使用它,但无法找出将以下查询转换为LINQ的正确语法:

select ChapterID, min(MeetingDate)
from ChapterMeeting
group by ChapterID

2 个答案:

答案 0 :(得分:33)

var query = myDataContext.ChapterMeeting
  .GroupBy(cm => cm.ChapterID)
  .Select(g => new {
      g.Key,
      MinMeetingDate = g.Min(cm => cm.MeetingDate)
  });

答案 1 :(得分:11)

好吧,大卫打败了我,但万一你想用“理解”语法来看待它:

var q = (
    from cm in context.ChapterMeeting
    group cm by cm.ChapterID into cmg
    select new {
        ChapterID = cmg.Key,
        FirstMeetingDate = cmg.Min(cm => cm.MeetingDate)});