How do I translate this GROUP BY / MIN SQL query into LINQ?

泄露秘密 提交于 2019-12-20 11:37:20

问题


I plan on using this in a subquery but can't figure out the correct syntax to translate the following query into LINQ:

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

回答1:


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



回答2:


Well, Amy beat me to it, but in case you wanted to see it with the "comprehension" syntax:

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


来源:https://stackoverflow.com/questions/654285/how-do-i-translate-this-group-by-min-sql-query-into-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!