LINQ to SQL: GroupBy() and Max() to get the object with latest date

后端 未结 3 1107
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 12:13

Consider a SQL Server table that\'s used to store events for auditing.

The need is to get only that latest entry for each CustID. We want to get the entire

3条回答
  •  囚心锁ツ
    2020-12-28 13:13

    Using LINQ syntax, which I think looks cleaner:

    var custsLastAccess = from c in db.CustAccesses 
                          group c by c.CustID into grp
                          select grp.OrderByDescending(c => c.AccessDate).FirstOrDefault();
    

提交回复
热议问题