使用Max在Linq中分组:如何获得最大值的完整数据集?(Group in Linq with Max: How do I get the full data set of the max value?)

到目前为止我得到的是:

var a = from e in tcdb.timeclockevent group e by e.workerId into r select new { workerId = r.Key, Date = r.Max(d => d.timestamp) };

此查询为我提供了每个workerId的最新“时间戳”(注意:workerId不是tcdb.timeclockevent的主键)。 所以它只给我两对值,但我需要整个数据集

有没有人知道如何使用每个workerId的最大时间戳获取tcdb.timeclock的整个数据集?

要么

有谁知道如何获得每个工人的最大日期数据集的ID?

先感谢您 :)

what I have got so far is this:

var a = from e in tcdb.timeclockevent group e by e.workerId into r select new { workerId = r.Key, Date = r.Max(d => d.timestamp) };

This Query is giving me latest "timestamp" of every workerId (Note: workerId is not the primary key of tcdb.timeclockevent). So it is only giving me pairs of two values but I need the whole data sets

Does anybody know how I can get the whole datasets of tcdb.timeclock with the maximal timestamp for every workerId?

OR

Does anybody know how I can get the Id of the data sets of the maximal date for each worker?

Thank you in advance :)

最满意答案

您可以按timestamp订购r分组并选择第一个

var a = from e in tcdb.timeclockevent group e by e.workerId into r select r.OrderByDescending(d => d.timestamp).FirstOrDefault();

You can order your r grouping by timestamp and select the first one

var a = from e in tcdb.timeclockevent group e by e.workerId into r select r.OrderByDescending(d => d.timestamp).FirstOrDefault();

更多推荐