title: Linq - Group by first letter of name
body: <p>I'm using Entity Framework 6 as my data source.</p>
  
  <p>I've created a page that will list suppliers by the first letter of their name, so first we have all the 'A's, then 'B's and so on.</p>
  
  <p>In order to do this, I use 2 ListView objects - it could easily be a Repeater, but that's not important.</p>
  
  <p>Although my list of suppliers is not extensive, the method I've used to get the data is quite expensive in that I have to call it 27 times during databinding. I'm pretty sure there is a better way of going about this but don't know my way around Linq well enough.</p>
  
  <p>I'm thinking there must be a way of grouping data and then looping through the content of a group.</p>
  
  <p>Here is the important bits of code. The linq to retreive the child data and the databinding code:</p>
  
  <pre><code>    public static IEnumerable&lt;Supplier&gt; StartsWith(string firstLetter)
      {
          return Select() // select simply returns all data for the entity
              .Where(x =&gt; x.Name.StartsWith(firstLetter, StringComparison.OrdinalIgnoreCase));
      }
  
      protected void ListViewAtoZ_ItemDataBound(object source, ListViewItemEventArgs e)
      {
          var item = e.Item;
  
          if (item.ItemType == ListViewItemType.DataItem)
          {
              var alphanumeric = (string)item.DataItem;
  
              var h2 = item.GetControl&lt;HtmlGenericControl&gt;("HtmlH2", true);
              h2.InnerText = alphanumeric;
  
              var childView = item.GetControl&lt;ListView&gt;("ListViewStartsWith", true);
              childView.DataSource = LenderView.StartsWith(alphanumeric);
              childView.DataBind();
          }
      }
  
      protected void ListViewStartsWith_ItemDataBound(object source, ListViewItemEventArgs e)
      {
          var item = e.Item;
  
          if (item.ItemType == ListViewItemType.DataItem)
          {
              var supplier = (Supplier)item.DataItem;
  
              var litName = item.GetControl&lt;Literal&gt;("LiteralName", true);
              litName.Text = supplier.Name;
          }
      }
  
      void LoadData()
      {
          var alphanumerics = new string[]
          {
              "0 - 9","A","B","C","D","E","F","G","H","I","J","K","L",
              "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
          }; 
  
          ListViewAtoZ.DataSource = alphanumerics;
          ListViewAtoZ.DataBind();
      }
  </code></pre>