C# 如何使用 LINQ 计算列表中的重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/454601/
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-04 04:00:33  来源:igfitidea点击:

How to Count Duplicates in List with LINQ

c#linq

提问by SpoiledTechie.com

I have a list of items

我有一个物品清单

  • John ID
  • Matt ID
  • John ID
  • Scott ID
  • Matt ID
  • John ID
  • Lucas ID
  • 约翰身份证
  • 马特身份证
  • 约翰身份证
  • 斯科特身份证
  • 马特身份证
  • 约翰身份证
  • 卢卡斯身份证

I want to shove them back into a list like so which also means I want to sort by the highest number of duplicates.

我想将它们推回到这样的列表中,这也意味着我想按重复次数最多的顺序排序。

  • John ID 3
  • Matt ID 2
  • Scott ID 1
  • Lucas ID 1
  • 约翰 ID 3
  • 马特 ID 2
  • 斯科特 ID 1
  • 卢卡斯 ID 1

Let me know how I can do this with LINQ and C#.

让我知道如何使用 LINQ 和 C# 做到这一点。

Thanks All

谢谢大家

EDIT 2 Showing Code:

编辑 2 显示代码:

    List<game> inventory = new List<game>();
    drinkingforDataContext db = new drinkingforDataContext();
    foreach (string item in tbTitle.Text.Split(' '))
    {

        List<game> getItems = (from dfg in db.drinkingfor_Games
                               where dfg.game_Name.Contains(tbTitle.Text)
                               select new game
                               {
                                   gameName = dfg.game_Name,
                                   gameID = Boomers.Utilities.Guids.Encoder.EncodeURLs(dfg.uid)
                               }).ToList<game>();

        for (int i = 0; i < getItems.Count(); i++)
        {
            inventory.Add(getItems[i]);
        }
    }

    var items = (from xx in inventory
                 group xx by xx into g
                 let count = g.Count()
                 orderby count descending
                 select new
                    {
                        Count = count,
                        gameName = g.Key.gameName,
                        gameID = g.Key.gameID
                    });

    lvRelatedGames.DataSource = items;
    lvRelatedGames.DataBind();

This query displays these results:

此查询显示以下结果:

  • 1 hello world times
  • 1 hello world times
  • 1 Hello World.
  • 1 hello world times
  • 1 hello world times
  • 1 hello world times
  • 1 Hello World.
  • 1 hello world times
  • 1 你好世界时报
  • 1 你好世界时报
  • 1 世界你好。
  • 1 你好世界时报
  • 1 你好世界时报
  • 1 你好世界时报
  • 1 世界你好。
  • 1 你好世界时报

It gives me the count and name, but it doesn't give me the ID of the game....

它给了我计数和名称,但它没有给我游戏的 ID....

It should display:

它应该显示:

  • 6 hello world times 234234
  • 2 Hello World. 23432432
  • 6 你好世界时报 234234
  • 2 世界你好。23432432

采纳答案by aku

You can use "group by" + "orderby". See LINQ 101for details

您可以使用“group by”+“orderby”。有关详细信息,请参阅LINQ 101

var list = new List<string> {"a", "b", "a", "c", "a", "b"};
var q = from x in list
        group x by x into g
        let count = g.Count()
        orderby count descending
        select new {Value = g.Key, Count = count};
foreach (var x in q)
{
    Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);
}

In response to this post(now deleted):

回复这篇文章(现已删除):

If you have a list of some custom objects then you need to use custom compareror group by specific property.

如果您有一些自定义对象的列表,那么您需要使用自定义比较器或按特定属性分组。

Also query can't display result. Show us complete code to get a better help.

查询也无法显示结果。向我们展示完整的代码以获得更好的帮助。

Based on your latest update:

根据您的最新更新:

You have this line of code:

你有这行代码:

group xx by xx into g

Since xx is a custom object system doesn't know how to compare one item against another. As I already wrote, you need to guide compiler and provide some property that will be used in objects comparison or provide custom comparer. Here is an example:

由于 xx 是一个自定义对象系统不知道如何将一个项目与另一个项目进行比较。正如我已经写过的,您需要引导编译器并提供一些将用于对象比较的属性或提供自定义比较器。下面是一个例子:

Note that I use Foo.Nameas a key - i.e. objects will be grouped based on value of Nameproperty.

请注意,我使用Foo.Name作为键 - 即对象将根据Name属性的值进行分组。

There is one catch - you treat 2 objects to be duplicate based on their names, but what about Id ? In my example I just take Id of the first object in a group. If your objects have different Ids it can be a problem.

有一个问题 - 您根据名称将 2 个对象视为重复对象,但是 Id 呢?在我的示例中,我只获取组中第一个对象的 ID。如果您的对象具有不同的 ID,则可能会出现问题。

//Using extension methods
var q = list.GroupBy(x => x.Name)
            .Select(x => new {Count = x.Count(), 
                              Name = x.Key, 
                              ID = x.First().ID})
            .OrderByDescending(x => x.Count);

//Using LINQ
var q = from x in list
        group x by x.Name into g
        let count = g.Count()
        orderby count descending
        select new {Name = g.Key, Count = count, ID = g.First().ID};

foreach (var x in q)
{
    Console.WriteLine("Count: " + x.Count + " Name: " + x.Name + " ID: " + x.ID);
}

回答by CMS

Slightly shorter version using methods chain:

使用方法链的略短版本:

var list = new List<string> {"a", "b", "a", "c", "a", "b"};
var q = list.GroupBy(x => x)
            .Select(g => new {Value = g.Key, Count = g.Count()})
            .OrderByDescending(x=>x.Count);

foreach (var x in q)
{
    Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);
}

回答by Colonel Panic

The other solutions use GroupBy. GroupByis slow (it holds all the elements in memory) so I wrote my own method CountBy:

其他解决方案使用GroupBy. GroupBy很慢(它将所有元素保存在内存中)所以我编写了自己的方法CountBy

public static Dictionary<TKey,int> CountBy<TSource,TKey>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector)
{
    var countsByKey = new Dictionary<TKey,int>();
    foreach(var x in source)
    {
        var key = keySelector(x);
        if (!countsByKey.ContainsKey(key))
            countsByKey[key] = 0;
        countsByKey[key] += 1;
    }
    return countsByKey;
}

回答by Debendra Dash

Here is the complete programme please check this

这是完整的程序,请检查这个

static void Main(string[] args)
{
    List<string> li = new List<string>();
    li.Add("Ram");
    li.Add("shyam");
    li.Add("Ram");
    li.Add("Kumar");
    li.Add("Kumar");

    var x = from obj in li group obj by obj into g select new { Name = g.Key, Duplicatecount = g.Count() };
    foreach(var m in x)
    {
        Console.WriteLine(m.Name + "--" + m.Duplicatecount);
    }
    Console.ReadLine();
}        

回答by Willy David Jr

You can also do Dictionary:

你也可以做字典:

 var list = new List<string> { "a", "b", "a", "c", "a", "b" };
 var result = list.GroupBy(x => x)
            .ToDictionary(y=>y.Key, y=>y.Count())
            .OrderByDescending(z => z.Value);

 foreach (var x in result)
        {
            Console.WriteLine("Value: " + x.Key + " Count: " + x.Value);
        }