c# - How to select last record in a LINQ GroupBy clause -
i have following simple table id , contactid , comment . i want select records , groupby contactid . used linq extension method statement: mains.groupby(l => l.contactid) .select(g => g.firstordefault()) .tolist() it returns record 1 , 4 . how can use linq contactid highest id ? (i.e. return 3 , 6 ) you can order items mains.groupby(l => l.contactid) .select(g=>g.orderbydescending(c=>c.id).firstordefault()) .tolist()