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>();
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?
source
share