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
  • 178,213
  • 47
  • 333
  • 501
Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93

6 Answers6

22

Try this:

var numberOfTestcasesWithDuplicates = 
    scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Count() > 1);
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
8

As a first idea:

int dupes = list.Count() - list.Distinct(aTestCaseComparer).Count();
H H
  • 263,252
  • 30
  • 330
  • 514
  • @daryal - a better definition of equal (dupe) will be needed. As I tried to say, it's a rough sketch that solves the counting issue. – H H Mar 28 '12 at 14:27
  • 1
    This will not work out of the box - you omitted the need to implement either a custom Equality comparer or override equality within OP's class – BrokenGlass Mar 28 '12 at 14:29
  • @broken - inserted a placeholder. – H H Mar 28 '12 at 14:32
5

To just get the duplicate count:

int duplicateCount = scenarios.GroupBy(x => x.ScenarioID)
                              .Sum(g => g.Count()-1);
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
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
  • 70,104
  • 56
  • 326
  • 368
Servy
  • 202,030
  • 26
  • 332
  • 449
2

Something like this maybe

var result= GetTestCases()
            .GroupBy (x =>x.ScenarioID)
            .Select (x =>new{x.Key,nbrof=x.Count ()} );
Arion
  • 31,011
  • 10
  • 70
  • 88
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
  • 70,104
  • 56
  • 326
  • 368