Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding combinations of a grouped list using LINQ in c#

Tags:

c#

linq

Lets say I have an object which contains a persons name, and their city of origin.

public class personDetails
{
    public string City;
    public string Name;
}

And I have a List with the following entries added.

Name   City
John | London
Jane | London
Tom  | New York
Bob  | New York
Fred | New York

What I'm looking for is all possible combinations of names, grouped by city.

John Tom
John Bob  
John Fred
Jane Tom
Jane Bob
Jane Fred

I can do this if I know in advance the number of groups, by using the following code

List<personDetails> personList = new List<personDetails>();
//populate list

var groupedPersons = personList.GroupBy(c => c.City);
foreach (var item1 in groupedPersons[0])
{
    foreach (var item2 in groupedPersons[1])
    {
        Console.WriteLine(item1.Name + " " + item2.Name);
    }          
}

However, this only works if i know the number of groups in advance, and quickly becomes unwieldy as the amount of groups grow larger. I'm sure that there is an elegant way to do this using LINQ, can anybody shed some light?

like image 479
John Avatar asked Oct 22 '22 21:10

John


1 Answers

We'll start with the following snippet of code taken verbatum from here. (It's a good link, worth reading).

public static class MyExtensions
{
    public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
    {
        IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
        return sequences.Aggregate(
            emptyProduct,
            (accumulator, sequence) =>
            from accseq in accumulator
            from item in sequence
            select accseq.Concat(new[] { item }));
    }
}

After that all we need to do is:

var groupedPersons = personList.GroupBy(c => c.City)
    //need an enumerable of enumerables, not an enumerable of groupings, 
    //because the method isn't covariant.
    .Select(group => group.AsEnumerable());

var results = groupedPersons.CartesianProduct();
foreach (var group in results)
{
    foreach (var person in group)
    {
        Console.Write(person.Name + " ");
    }
    System.Console.WriteLine();
}
like image 63
Servy Avatar answered Oct 27 '22 10:10

Servy