Wednesday 7 March 2018

Using Linq to count how many times an object occurs in a list


You have a list full of objects. Some of the objects are duplicates, and you want know know how many of each distinct item you have in the list.

I searched around for some answers, and I did find some StackOverflow answers that almost got me to the result, but it wasn’t quite right.

In my example, the list I want to sort is a list of custom type Run ie List<Run>(). In my code, Run is a struct that looks like:

public struct Run
{
     public string RunID;
     public string Config;
}

The StackOverflow responses were mostly long and hard-to-read Linq statements. I like Linq a lot, but it’s not always easy on the eyes, especially once you start adding extensions.

Instead, I came up with the following:

void ProcessList (List<Run> runConfigs)
{
     var grouping = runConfigs.GroupBy(o => o.Config);
     foreach (var configGroup in grouping)
     {
          string configName = configGroup.Key.ToString();   
          string count = configGroup.Count<Run>().ToString();
     }
}

Doing the above code allows me to use configName and count however I please (which in my case was outputting them to a csv).

Here’s the same example but for a List<string>():

var grouping = listOfStrings.GroupBy(x => x);
foreach (var stringGroup in grouping)
{
    string name = stringGroup.Key.ToString();
    string count = stringGroup.Count().ToString();
}

If you step through this code with the debugger, it will show that 'name' is one of the distinct values in the list, and 'count' is how many times that object appears in the list. These values can now be used however you like.

No comments:

Post a Comment