Finding grouped list combinations using LINQ in C #

Suppose I have an object that contains the name of a person and his city of origin.

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

And I have a list with the following additions.

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

I am looking for 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 the number of groups in advance 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 cumbersome as the number of groups increases. I'm sure there is an elegant way to do this with LINQ, can anyone shed some light?

+5
source share
1 answer

We will start with the following code snippet taken from here . (This is 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();
}
+3
source

All Articles