开发者

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) 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜