I would like to group a list of data per a key, and find the max per a different property. If there is a tie, I would find the max among the ties by another property.
public class Model { public int Id { get; set; } public string GroupKey { get; set; } public string MaxKey { get; set; } } /********** DATA ***************/ Id GroupKey MaxKey 1 groupA a 2 groupA b 3 groupB c 4 groupB c I would like to get the models with the below Ids:
- 2 because within
groupA,2has a biggerMaxKeyofb - 4 because within
groupB, both3and4have the sameMaxKeyofcbut4has a biggerId
What I have now is:
from model in list group model by model.GroupKey into grouped select new { Key = grouped.Key, Max = grouped.Max(g => g.MaxKey) } I am not sure how to do the 2nd max in the syntax. Can anyone please help?
Constraints:
- The code needs to be able to run in the database.
- Please do not use the expression syntax.
没有评论:
发表评论