How to get top 3 from each "by group" in LINQ?
I have a table that looks something like:
Favourite color | Favourite Food | Favourite Dance | Date
Now I want to group by favourite color and favourite food. Then take top 3 in each group ord开发者_如何学运维ered by date (the latest). I just cannot seem to get it to work using LINQ.
Like this:
from x in thingy
group x by new { x.Color, x.Food } into g
select new {
g.Key.Color,
g.Key.Food,
Items = g.OrderByDescending(x => x.Date).Take(3)
}
精彩评论