title: Finding combinations of a grouped list using LINQ in c#
body: <p>Lets say I have an object which contains a persons name, and their city of origin.</p>
  
  <pre><code>public class personDetails
  {
      public string City;
      public string Name;
  }
  </code></pre>
  
  <p>And I have a List with the following entries added.</p>
  
  <pre><code>Name   City
  John | London
  Jane | London
  Tom  | New York
  Bob  | New York
  Fred | New York
  </code></pre>
  
  <p>What I'm looking for is all possible combinations of names, grouped by city.</p>
  
  <pre><code>John Tom
  John Bob  
  John Fred
  Jane Tom
  Jane Bob
  Jane Fred
  </code></pre>
  
  <p>I can do this if I know in advance the number of groups, by using the following code</p>
  
  <pre><code>List&lt;personDetails&gt; personList = new List&lt;personDetails&gt;();
  //populate list
  
  var groupedPersons = personList.GroupBy(c =&gt; c.City);
  foreach (var item1 in groupedPersons[0])
  {
      foreach (var item2 in groupedPersons[1])
      {
          Console.WriteLine(item1.Name + " " + item2.Name);
      }          
  }
  </code></pre>
  
  <p>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?</p>