开发者

How to get top 3 from each "by group" in LINQ?

开发者 https://www.devze.com 2023-04-01 11:52 出处:网络
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开发者

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

精彩评论

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

关注公众号