gpt4 book ai didi

c# - 获取 Entity Framework 中每个组的最后记录?

转载 作者:太空狗 更新时间:2023-10-29 22:15:38 25 4
gpt4 key购买 nike

我想检索 Entity Framework 中每个 MobileNo 的最后插入记录。

这是我的表格数据。

ID      RegNo       MobileNo    CreatedDate
26727 190077348 9696562673 13-02-2017 06:31
26729 123782783 9696562673 13-02-2017 06:35
45779 530087328 5878525875 07-02-2017 07:23
99902 120058572 7379130560 08-02-2017 12:39
64477 180073650 7417516480 10-02-2017 13:47
81839 240087264 7754990580 11-02-2017 10:47

并且想要这样的输出

ID      RegNo       MobileNo    CreatedDate 
26729 123782783 9696562673 13-02-2017 06:35
45779 530087328 5878525875 07-02-2017 07:23
99902 120058572 7379130560 08-02-2017 12:39
64477 180073650 7417516480 10-02-2017 13:47
81839 240087264 7754990580 11-02-2017 10:47

最佳答案

假设您的表的名称是Items:

var result = dbContext.Items.GroupBy(x => x.MobileNo)
.Select(x => x.OrderByDescending(y => y.CreatedDate).First());

运行示例:https://dotnetfiddle.net/3ud2pB

关于c# - 获取 Entity Framework 中每个组的最后记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42274806/

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