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

2020-05-25 06:35发布

问题:

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