C# 分组收集,每组取前5个
我有一个收藏。我需要按“A”属性对集合进行分组。我必须按“B”属性对每个组进行排序。然后从每组中选择前5个 有人能为这个问题提出LINQ查询吗 我尝试的方式不起作用C# 分组收集,每组取前5个,c#,linq,C#,Linq,我有一个收藏。我需要按“A”属性对集合进行分组。我必须按“B”属性对每个组进行排序。然后从每组中选择前5个 有人能为这个问题提出LINQ查询吗 我尝试的方式不起作用 (from item in Recipes orderby item.Rating descending group item by item.MainCategory).Take(5) 查询应返回IEnumerable编辑:在您的情况下,添加排序(在OP更新后): 你参加的是前五组。相反,您需要从每组中选择前五项: fro
(from item in Recipes
orderby item.Rating descending
group item by item.MainCategory).Take(5)
查询应返回
IEnumerable
编辑:在您的情况下,添加排序(在OP更新后):
你参加的是前五组。相反,您需要从每组中选择前五项:
from item in Recipes
orderby item.Rating descending
group item by item.MainCategory into g
select g.Take(5)
更新:
from item in Recipes
orderby item.Rating descending
group item by item.MainCategory into g
select g.Take(5).GroupBy(item => item.MainCategory).First()
你需要
GroupBy
、OrderBy
和Take
操作员OP已经证明了他尝试这一点的尝试,我不知道问题出在哪里?@DeeMac看看上次编辑它的时间和留下评论的时间……这个问题的原始问题似乎已经解决了。为什么这么多反对票。我仍然没有得到一个好的建议。这不会返回组。@XAMLLover是的,这将返回IEnumerable
,而不是group@XAMLLover更新答案。但是我不明白你为什么需要在这里进行分组。
from item in Recipes
orderby item.Rating descending
group item by item.MainCategory into g
select g.Take(5).GroupBy(item => item.MainCategory).First()