查找 C# 列表中重复项的数量

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

Find the count of duplicate items in a C# List

c#list

提问by Priyank Thakkar

I am using List in C#. Code is as mentioned below:

我在 C# 中使用 List。代码如下:

TestCase.cs

测试用例

 public class TestCase
{
    private string scenarioID;
    private string error;

    public string ScenarioID
    {
        get
        {
            return this.scenarioID;
        }
        set
        {
            this.scenarioID = value;
        }
    }

    public string Error
    {
        get
        {
            return this.error;
        }
        set
        {
            this.error = value;
        }
    }

    public TestCase(string arg_scenarioName, string arg_error)
    {
        this.ScenarioID = arg_scenarioName;
        this.Error = arg_error;
    }
}

List I am createing is:

我正在创建的列表是:

private List<TestCase> GetTestCases()
    {
        List<TestCase> scenarios = new List<TestCase>();
        TestCase scenario1 = new TestCase("Scenario1", string.Empty);
        TestCase scenario2 = new TestCase("Scenario2", string.Empty);
        TestCase scenario3 = new TestCase("Scenario1", string.Empty);
        TestCase scenario4 = new TestCase("Scenario4", string.Empty);
        TestCase scenario5 = new TestCase("Scenario1", string.Empty);
        TestCase scenario6 = new TestCase("Scenario6", string.Empty);
        TestCase scenario7 = new TestCase("Scenario7", string.Empty);

        scenarios.Add(scenario1);
        scenarios.Add(scenario2);
        scenarios.Add(scenario3);
        scenarios.Add(scenario4);
        scenarios.Add(scenario5);
        scenarios.Add(scenario6);
        scenarios.Add(scenario7);

        return scenarios;
    }

Now I am iterating through the list. I want to find the how many duplicate testcases are there in a list with same ScenarioID. Is there any way to solve it using Linq or any inbuilt method for List?

现在我正在遍历列表。我想找到具有相同 ScenarioID 的列表中有多少重复的测试用例。有没有办法使用 Linq 或 List 的任何内置方法来解决它?

Regards, Priyank

问候, 普里扬克

采纳答案by Daniel Hilgarth

Try this:

尝试这个:

var numberOfTestcasesWithDuplicates = 
    scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Count() > 1);

回答by Henk Holterman

As a first idea:

作为第一个想法:

int dupes = list.Count() - list.Distinct(aTestCaseComparer).Count();

回答by Servy

var groups = scenarios.GroupBy(test => test.ScenarioID)
    .Where(group => group.Skip(1).Any());

That will give you a group for each ScenarioID that has more than one items. The count of the groups is the number of duplicate groups, and the count of each group internally is the number of duplicates of that single item.

这将为每个具有多个项目的 ScenarioID 提供一个组。组的计数是重复组的数量,每个组内部的计数是该单个项目的重复数量。

Additional note, the .Skip(1).Any()is there because a .Count()in the Whereclause would need to iterate every single item just to find out that there is more than one.

附加说明,.Skip(1).Any()是因为有一.Count()Where条款将需要遍历每一个项目刚刚发现,有不止一个。

回答by BrokenGlass

To just get the duplicate count:

要获得重复计数

int duplicateCount = scenarios.GroupBy(x => x.ScenarioID)
                              .Sum(g => g.Count()-1);

回答by Arion

Something like this maybe

可能是这样的

var result= GetTestCases()
            .GroupBy (x =>x.ScenarioID)
            .Select (x =>new{x.Key,nbrof=x.Count ()} );

回答by nawfal

To get total number of duplicates, yet another:

要获得重复的总数,还有另一个:

var set = new HashSet<string>();
var result = scenarios.Count(x => !set.Add(x.ScenarioID));

To get distinct duplicates:

要获得不同的重复项:

var result = scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Skip(1).Any());