如何将列表缩小到 C# 中相同项目的最大数量
How to narrow list down to a max number of same items in C#
假设我将这个列表放在一个名为 'array':
的数组中
[0]a.1
[1]b.1
[2]c.1
[3]d.1
[4]e.2
[5]f.2
[6]g.2
[7]h.3
我想用 C# 将其缩小到一个列表中最多有两个相同数字的列表,因此它看起来像这样:
[0]a.1
[1]b.1
[2]e.2
[3]f.2
[4]h.3
我正在尝试使用 'GroupBy':
var Groups = array.GroupBy(i => i);
var Result = Groups.SelectMany(iGroup => iGroup.Take(2)).ToArray();
但我不确定如何只考虑点后面的内容而不是整个项目
I'm not sure how to only take what's after the dot into consideration and not the whole item
如果点保证存在,在点上拆分,取第二项:
var Groups = array.GroupBy(i => i.Split('.')[1]);
您的其余代码,SelectMany
和 Take(2)
是正确的。
假设我将这个列表放在一个名为 'array':
的数组中[0]a.1
[1]b.1
[2]c.1
[3]d.1
[4]e.2
[5]f.2
[6]g.2
[7]h.3
我想用 C# 将其缩小到一个列表中最多有两个相同数字的列表,因此它看起来像这样:
[0]a.1
[1]b.1
[2]e.2
[3]f.2
[4]h.3
我正在尝试使用 'GroupBy':
var Groups = array.GroupBy(i => i);
var Result = Groups.SelectMany(iGroup => iGroup.Take(2)).ToArray();
但我不确定如何只考虑点后面的内容而不是整个项目
I'm not sure how to only take what's after the dot into consideration and not the whole item
如果点保证存在,在点上拆分,取第二项:
var Groups = array.GroupBy(i => i.Split('.')[1]);
您的其余代码,SelectMany
和 Take(2)
是正确的。