Get the ranking of highest score with earliest timestamp

Posted by Billy on Stack Overflow See other posts from Stack Overflow or by Billy
Published on 2010-12-24T05:36:19Z Indexed on 2010/12/24 5:54 UTC
Read the original article Hit count: 368

Filed under:

I have the following records:

   name   score Date
   Billy  32470 12/18/2010 7:26:35 PM
   Sally  1100  12/19/2010 12:00:00 AM
   Kitty  1111  12/21/2010 12:00:00 AM
   Sally  330   12/21/2010 8:23:34 PM
   Daisy  32460 12/22/2010 3:10:09 PM
   Sally  32460 12/23/2010 4:51:11 PM
   Kitty  32440 12/24/2010 12:00:27 PM
   Billy  32460 12/24/2010 12:11:36 PM

I want to get the leaderboard of the highest score with earliest time stamp using LINQ. In this case, the correct one is

rank name 
  1  Billy 
  2  Daisy 
  3  Sally 

I use the following query:

var result = 
(from s in Submissions
group s by s.name into g
orderby g.Max(q => q.Score) descending,g.Min(q => q.Date) ascending
select new ScoreRecord
{
name = g.Key
Score = g.Max(q => q.Score)
}).Take(3).ToList();

I get the following wrong result:

rank name 
  1  Billy 
  2  Sally
  3  Daisy

What's the correct linq query in this case?

© Stack Overflow or respective owner

Related posts about LINQ