Linq查询来对每个组中的顶级项目进行分组和查询(Linq query to group items and query from the top item in each group)
所以,我有一个看起来像这样的列表。 它基本上是一堆物品的状态历史记录,最近的状态代表当前状态。
Record Id State Date ===== === ========= ======= 1 A Waiting Jan 01 2 A InProgress Jan 02 3 A Finished Jan 03 4 B Waiting Jan 02 5 C Waiting Jan 01 6 C InProgress Jan 02 7 D Waiting Jan 01 8 D InProgress Jan 02我正在寻找的是能够查询每个项目的“当前”状态。 例如,我想说:“给我所有的Ids都是'InProgress'”并且获得Id D和Id C,但不是Id A(因为它的最新状态是'已完成')。
我知道我必须做一些分组和一些订购或Maxing,但我不能把它放在一起。
So, I have a list that looks something like this. Its basically a state history for a bunch of items, with the most recent state representing the current state.
Record Id State Date ===== === ========= ======= 1 A Waiting Jan 01 2 A InProgress Jan 02 3 A Finished Jan 03 4 B Waiting Jan 02 5 C Waiting Jan 01 6 C InProgress Jan 02 7 D Waiting Jan 01 8 D InProgress Jan 02What I'm looking for is to be able to query the 'current' state for each item. For example, I'd like to say: "Give me all Ids are are 'InProgress'" and get Id D and Id C, but not Id A (because its latest state is 'Finished').
I know I've got to do some grouping and some ordering or Maxing, but I can't quite put it all together.
最满意答案
myList.GroupBy(m => m.Id) .Select(g => g.OrderByDescending(x => x.Date).First()) .Where(<your filter>); myList.GroupBy(m => m.Id) .Select(g => g.OrderByDescending(x => x.Date).First()) .Where(<your filter>);更多推荐
发布评论