5

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

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?

Regards, Priyank

Robert Harvey
  • 173,679
  • 45
  • 326
  • 490
Priyank Thakkar
  • 4,532
  • 17
  • 52
  • 90

6 Answers6

20

Try this:

var numberOfTestcasesWithDuplicates = 
    scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Count() > 1);
Daniel Hilgarth
  • 166,158
  • 40
  • 312
  • 426
8

As a first idea:

int dupes = list.Count() - list.Distinct(aTestCaseComparer).Count();
Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
4
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.

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

nawfal
  • 66,413
  • 54
  • 311
  • 354
Servy
  • 197,813
  • 25
  • 319
  • 428
4

To just get the duplicate count:

int duplicateCount = scenarios.GroupBy(x => x.ScenarioID)
                              .Sum(g => g.Count()-1);
BrokenGlass
  • 153,880
  • 28
  • 280
  • 327
2

Something like this maybe

var result= GetTestCases()
            .GroupBy (x =>x.ScenarioID)
            .Select (x =>new{x.Key,nbrof=x.Count ()} );
Arion
  • 30,443
  • 10
  • 68
  • 86
1

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());
nawfal
  • 66,413
  • 54
  • 311
  • 354