How do I translate this GROUP BY / MIN SQL query i

2020-05-25 06:14发布

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

2条回答
Ridiculous、
2楼-- · 2020-05-25 06:29

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)});
查看更多
走好不送
3楼-- · 2020-05-25 06:34
var query = myDataContext.ChapterMeeting
  .GroupBy(cm => cm.ChapterID)
  .Select(g => new {
      g.Key,
      MinMeetingDate = g.Min(cm => cm.MeetingDate)
  });
查看更多
登录 后发表回答