C# 获取不同项目及其计数的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2010556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:38:25  来源:igfitidea点击:

Get a list of distinct items and their count

c#asp.netlinqlinq-to-objects

提问by Clarence Klopfstein

I have an object, that has many properties but the only two to worry about are:

我有一个对象,它有很多属性,但唯一需要担心的是:

myobject.IDwhich is an int
myobject.Nameswhich is a HashSet

myobject.ID这是一个int
myobject.Names这是一个HashSet

Then I have a Listof those objects that looks something similar to this:

然后我有一个List看起来与此类似的对象:

List<myobject>

I use Linq to get a some of the data into a repeater, but I'm not sure how to get the list of Names and how often they show up.

我使用 Linq 将一些数据放入转发器,但我不确定如何获取名称列表以及它们出现的频率。

Want to use Linq to avoid having to loop through the data.

想使用 Linq 来避免遍历数据。

As my tags should show, this is an ASP.NET solution using C#.

正如我的标签所示,这是一个使用 C# 的 ASP.NET 解决方案。

Some clarification:
Lets say I have just three items in my list: Item 1 has John, Fred, Hyman in its names. Item 2 has John, Fred, Joe in its names. Item 3 has John in its names.

一些澄清:
假设我的列表中只有三个项目:项目 1 的名字中有 John、Fred、Hyman。第 2 项的名称中包含 John、Fred、Joe。第 3 项的名字中有约翰。

I am trying to return the following: John - 3 Fred - 2 Hyman - 1 Joe - 1

我正在尝试返回以下内容:John - 3 Fred - 2 Hyman - 1 Joe - 1

Also, as a note I am familiar with having to write my own comparer for my object, I'm just missing the 'how to' for the overall solution in my thoughts.

另外,作为一个注释,我熟悉必须为我的对象编写自己的比较器,我只是在我的想法中缺少整体解决方案的“如何”。

采纳答案by jason

Here is how I understand the problem that you are trying to solve. You have a list of myobjects. Each myobjecthas a property called Nameswhich is a HashSetof strings (i.e., HashSet<string>). You want to count the number of times each stringthat appears in some myobject.Namesappears in all the myobject.Names. That is, you have

以下是我对您试图解决的问题的理解。你有一个myobjects的列表。每个myobject有一个称为属性Names是一个HashSetstringS(即,HashSet<string>)。你想计算每个string出现在 somemyobject.Names出现在所有myobject.Names. 也就是说,你有

"Alice", "Bob", "Charlie"
"Alice", "Bob", "Donald"
"Alice", "Donald", "Ernie"

as three myobject.Namesand you want to see

作为三myobject.Names,你想看到

"Alice", 3
"Bob", 2
"Charlie", 1
"Donald", 2
"Ernie", 1

If so:

如果是这样的话:

var query = list.SelectMany(x => x.Names)
                .GroupBy(s => s)
                .Select(g => new { Name = g.Key, Count = g.Count() });

foreach(var result in query) {
    Console.WriteLine("Name: {0}, Count: {1}", result.Name, result.Count);
}

I do not see what role myobject.IDplays here. Please qualify.

我看不出myobject.ID这里有什么作用。请符合条件。

回答by LukeH

var query = yourList
            .SelectMany(x => x.Names)
            .GroupBy(x => x, (y, z) => new { Name = y, Count = z.Count() });

// and to test...
foreach (var item in query)
{
    Console.WriteLine("{0} - {1}", item.Name, item.Count);
}