gpt4 book ai didi

c# - LINQ:获取列表中具有最大属性值的记录

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:04 25 4
gpt4 key购买 nike

我不知道它有多简单,

我有一个像这样的对象

public class ActionRecord
{
public int RecID { get; set; }
public string Action { get; set; }
public string ActArea { get; set; }
public int UserID { get; set; }
public DateTime ActDate { get; set; }
}

ActionRecord 中的示例数据可以如下所示:

----------------------------------------------------------------------
RecID Action ActArea UserID ActDate
---------------------------------------------------------------------
1 Edit Main 1 Item 5 2014-11-01 10:01:14
2 Delete Sub 1 Item 2 2014-11-01 14:22:58
3 Add Sub 5 Item 2 2014-11-01 15:16:03
4 Add Sub 6 Item 2 2014-11-01 15:48:32
5 Edit Main 1 Item 2 2014-11-01 16:04:50
7 Edit Main 2 Item 5 2014-11-01 16:19:03
8 Delete Main 2 Item 5 2014-11-01 16:38:49
9 Add Main 3 Item 2 2014-11-01 16:42:25
10 Edit Main 3 Item 3 2014-11-01 16:45:38

我的要求是,在 LINQ 的帮助下,我需要过滤数据,以便我只能获得 ActArea 上的最新操作,最终数据应该如下所示:

----------------------------------------------------------------------
RecID Action ActArea UserID ActDate
---------------------------------------------------------------------
10 Edit Main 3 Item 3 2014-11-01 16:45:38
8 Delete Main 2 Item 5 2014-11-01 16:38:49
5 Edit Main 1 Item 2 2014-11-01 16:04:50
4 Add Sub 6 Item 2 2014-11-01 15:48:32
3 Add Sub 5 Item 2 2014-11-01 15:16:03
2 Delete Sub 1 Item 2 2014-11-01 14:22:58

最佳答案

您可以使用 GroupBy 来做到这一点:

var res = dara.GroupBy(d=>d.ActArea)
.Select(g=>g.OrderBydescending(a=>a.ActDate).First());

关于c# - LINQ:获取列表中具有最大属性值的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com