title: C# Groupby Linq and foreach
body: <p>I need a more efficient way of producing multiple files from my data group.
  Im using a <code>List&lt;MyObject&gt;</code> type and my object has some public properties in which I need to group the data by.</p>
  
  <p>I have heard of <em>Linq</em> and it sounds like something I could use. However Im not sure how to go about it.</p>
  
  <p>I need to produce a text file for each STATE, so grouping all the <code>MyObjects</code> (people) by state, then running a <code>foreach</code> look on them to build the TEXT file.</p>
  
  <pre><code>void Main()
  {
  
      List&lt;MyObject&gt;   lst = new List&lt;MyObject&gt;();
      lst.Add(new MyObject{ name = "bill", state = "nsw", url = "microsoft.com"});
      lst.Add(new MyObject{ name = "ted",  state = "vic", url = "apple.com"});
      lst.Add(new MyObject{ name = "jesse", state = "nsw", url = "google.com"});
      lst.Add(new MyObject{ name = "james", state = "qld", url = "toshiba.com"});
  
      string builder = "";
      foreach (MyObject item in myObjects)  {
  
          builder += item.name + "\r\n";
          builder += item.url + "\r\n" + "\r\n\r\n";
  
      }
  
  and out to the `StreamWriter` will be the filenames by state. 
  </code></pre>
  
  <p>In total for the above data I need 3 files;</p>
  
  <pre><code>-nsw.txt
  -vic.txt
  -qld.txt
  </code></pre>