title: LINQ Getting Distinct Data and looping through to get related data
body: <p>I'm new to Linq. I want to know whether this is the best way or are there any other ways to do this.</p>
  
  <p>I have a requirement where from a web service, I receive a list of items:</p>
  
  <pre><code>class Item {
  
    string ItemName { get; set;} 
    string GroupName { get; set; }
  }
  </code></pre>
  
  <p>I receive the following data:</p>
  
  <pre><code>ItemName: Item1; GroupName: A
  ItemName: Item2; GroupName: B
  ItemName: Item3; GroupName: B
  ItemName: Item4; GroupName: A
  ItemName: Item5; GroupName: A
  </code></pre>
  
  <p>Now I want to get all of the unique Groups in the list, and associate all the Items to that Group. So I made a class:</p>
  
  <pre><code>class Group {
     string GroupName { get; set; }
     List&lt;string&gt; Items { get; set; }
  }
  </code></pre>
  
  <p>So that there is a single group and all associated Items will be under the List.</p>
  
  <p>I made two LINQ statements:</p>
  
  <pre><code>var uniqueGroups = (from g in webservice
          where g.Group != null
          select g.GroupName).Distinct();
  </code></pre>
  
  <p>Then I loop through it</p>
  
  <pre><code>foreach (var gn in uniqueGroups)
  {
     var itemsAssociated = (from item in webservice 
                            where item.GroupName = gn.ToString()
                            select new {
                                 });
  }
  </code></pre>
  
  <p>and then I got the items, and save them to my object.</p>
  
  <p>Is this the best way to do this or are there any LINQ statement that can do all these in one go?</p>
  
  <p>Thanks.</p>