Home Ask Login Register

Developers Planet

Your answer is one click away!

Beardy Bear January 2017

Group in Linq with Max: How do I get the full data set of the max value?

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 :)

Answers


Aducci January 2017

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

Post Status

Asked in January 2017
Viewed 1,364 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life