How to select last record in a LINQ GroupBy clause

后端 未结 5 907
长发绾君心
长发绾君心 2020-12-11 03:06

I have the following simple table with ID, ContactId and Comment.

I want to select records and GroupBy contactId. I

5条回答
  •  爱一瞬间的悲伤
    2020-12-11 03:54

    OrderByDescending

    Mains.GroupBy(l => l.ContactID)
    .Select(g=>g.OrderByDescending(c=>c.ID).FirstOrDefault()) 
    .ToList()
    

    is your best solution

    It orders by the highest ID descending, which is pretty obvious from the name.

提交回复
热议问题