在linq中获取符合特定条件的最新记录

Get latest record with certain criteria in linq

我有以下结果集。如何获取符合条件的最新历史记录:linq 中的 Status = 'A'?分组是根据实体 ID 完成的,记录将根据历史 ID

降序排列
History ID  Entity ID   Status
2969        6957        I
2968        6957        A
2967        6957        A
2303        6957        I
1000        6958        A
55          6959        A
50          6959        I
45          6960        I
40          6960        A

结果应该给我

History ID  Entity ID   Status
1000        6958        A
55          6959        A

首先按实体分组,并为每个组获取 "latest" 条记录。最后检查状态 A:

histories.GroupBy(h => h.EntityId)
         .Select(g => g.OrderByDescending(h => h.HistoryID)
                       .First())
         .Where(h => h.Status == "A");