Linq查询来对每个组中的顶级项目进行分组和查询(Linq query to group items and query from the top item in each group)

编程入门 行业动态 更新时间:2024-10-25 01:29:51
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 02

What 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>);

更多推荐

本文发布于:2023-04-29 09:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   项目   query   Linq   top

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!