title: Using LINQ to group a list of objects
body: <p>I have an object:</p>
  
  <pre><code>public class Customer
  {
      public int ID { get; set; }
      public string Name { get; set; }
      public int GroupID { get; set; }
  }
  </code></pre>
  
  <p>I return a list that may look like the following:</p>
  
  <pre><code>List&lt;Customer&gt; CustomerList = new List&lt;Customer&gt;();
  CustomerList.Add( new Customer { ID = 1, Name = "One", GroupID = 1 } );
  CustomerList.Add( new Customer { ID = 2, Name = "Two", GroupID = 1 } );
  CustomerList.Add( new Customer { ID = 3, Name = "Three", GroupID = 2 } );
  CustomerList.Add( new Customer { ID = 4, Name = "Four", GroupID = 1 } );
  CustomerList.Add( new Customer { ID = 5, Name = "Five", GroupID = 3 } );
  CustomerList.Add( new Customer { ID = 6, Name = "Six", GroupID = 3 } );
  </code></pre>
  
  <p>I want to return a linq query which will look like</p>
  
  <pre><code>CustomerList
    GroupID =1
      UserID = 1, UserName = "UserOne", GroupID = 1
      UserID = 2, UserName = "UserTwo", GroupID = 1
      UserID = 4, UserName = "UserFour", GroupID = 1
    GroupID =2
      UserID = 3, UserName = "UserThree", GroupID = 2
    GroupID =3
      UserID = 5, UserName = "UserFive", GroupID = 3
      UserID = 6, UserName = "UserSix",
  </code></pre>
  
  <p>I tried from</p>
  
  <p><a href="https://stackoverflow.com/questions/2697253/using-linq-to-group-a-list-of-objects-into-a-new-grouped-list-of-list-of-objects">Using Linq to group a list of objects into a new grouped list of list of objects</a></p>
  
  <p>code</p>
  
  <pre><code>var groupedCustomerList = CustomerList
    .GroupBy(u =&gt; u.GroupID)
    .Select(grp =&gt; grp.ToList())
    .ToList();
  </code></pre>
  
  <p>works but does not give the desired output.</p>