如何获取最大时间戳并按 LINQ to Entities 中的另一个字段进行分组

发布于 2024-10-05 20:25:14 字数 868 浏览 4 评论 0原文

我有一个具有以下字段的实体 GUID 任务ID, GUID 子任务Id, DateTime Timestamp

TaskId 可以与 SubTaskId 一对多。它几乎就像一个复合键。

我想编写一个或多个表达式,从而得出每个 TaskId 中的最新值 即

TaskId SubTaskId 时间戳
1 A 2010-11-22 15:48:49.727
1 B 2010-11-22 16:24:51.117
2 C 2010-11-15 11:49:05.717
2 D 2010-11-15 14:02:11.467

将导致序列仅包含:
任务 ID 子任务 ID 时间戳
1 B 2010-11-22 16:24:51.117
2 D 2010-11-15 14:02:11.467

这个表达式可以很好地显示所有 TaskId 和时间戳,

var q1 = 
    from req in myEntity
    orderby req.TaskId
    select new
    {
        req.TaskId,
        req.SubTaskId,
        req.Timestamp
    };

但我只想显示每个 TaskId 的最新时间戳。我向表达式添加最大值的所有尝试都失败了。该帖子(http://stackoverflow.com/questions/2034800/how-do-you-find-the-group-wise-max-in-linq)似乎很接近,但我无法将其转化为有效的东西。也许需要一个foreach

I have an Entity with the following fields
GUID TaskId,
GUID SubTaskId,
DateTime Timestamp

The TaskId may be one-to-many with the SubTaskId. It's like a composite key almost.

I would like to write an expression or expressions resulting in the newest among each TaskId
i.e.

TaskId SubTaskId Timestamp
1 A 2010-11-22 15:48:49.727
1 B 2010-11-22 16:24:51.117
2 C 2010-11-15 11:49:05.717
2 D 2010-11-15 14:02:11.467

Would result in a sequence containing only:
TaskId SubTaskId Timestamp
1 B 2010-11-22 16:24:51.117
2 D 2010-11-15 14:02:11.467

This expression works just fine for showing all TaskId's and TimeStamps

var q1 = 
    from req in myEntity
    orderby req.TaskId
    select new
    {
        req.TaskId,
        req.SubTaskId,
        req.Timestamp
    };

but I would like to show only the latest Timestamp for each TaskId. All my attempts at adding a max to the expression fail. The posting (http://stackoverflow.com/questions/2034800/how-do-you-find-the-group-wise-max-in-linq) seemed close, but I couldn't translate it into something working. Perhaps a foreach is needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢好甜 2024-10-12 20:25:14

这个怎么样?

var q1 = 
    from req in myEntity
    group req by req.TaskId into g
    let topReq = g.OrderByDescending(r => r.Timestamp).FirstOrDefault()
    select new
    {
        topReq.TaskId,
        topReq.SubTaskId,
        topReq.Timestamp
    };

How about this?

var q1 = 
    from req in myEntity
    group req by req.TaskId into g
    let topReq = g.OrderByDescending(r => r.Timestamp).FirstOrDefault()
    select new
    {
        topReq.TaskId,
        topReq.SubTaskId,
        topReq.Timestamp
    };
心在旅行 2024-10-12 20:25:14
var tasks = new[] { 
new { TaskId = 1, SubTaskId = "A", Timestamp = DateTime.Parse("2010-11-22 15:48:49.727")},
new { TaskId = 1, SubTaskId = "B", Timestamp = DateTime.Parse("2010-11-22 16:24:51.117")},
new { TaskId = 2, SubTaskId = "C", Timestamp = DateTime.Parse("2010-11-15 11:49:05.717")},
new { TaskId = 2, SubTaskId = "D", Timestamp = DateTime.Parse("2010-11-15 14:02:11.467")}};


var newestTasks = 
    from task in tasks
    group task by task.TaskId into g
    select g.Where(t => t.Timestamp == g.Max(t2 => t2.Timestamp)).Single();

foreach (var task in newestTasks)
    Console.WriteLine("TaskId = {0}, SubTaskId = {1}, Timestamp = {2}", 
        task.TaskId, task.SubTaskId, task.Timestamp);
var tasks = new[] { 
new { TaskId = 1, SubTaskId = "A", Timestamp = DateTime.Parse("2010-11-22 15:48:49.727")},
new { TaskId = 1, SubTaskId = "B", Timestamp = DateTime.Parse("2010-11-22 16:24:51.117")},
new { TaskId = 2, SubTaskId = "C", Timestamp = DateTime.Parse("2010-11-15 11:49:05.717")},
new { TaskId = 2, SubTaskId = "D", Timestamp = DateTime.Parse("2010-11-15 14:02:11.467")}};


var newestTasks = 
    from task in tasks
    group task by task.TaskId into g
    select g.Where(t => t.Timestamp == g.Max(t2 => t2.Timestamp)).Single();

foreach (var task in newestTasks)
    Console.WriteLine("TaskId = {0}, SubTaskId = {1}, Timestamp = {2}", 
        task.TaskId, task.SubTaskId, task.Timestamp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文