title: Order by enum description
body: <p>I am working on an ASP.NET MVC projet using EF code first, and I am facing a situation where I need to order by an enum description:</p>
  
  <pre><code>public partial class Item
  {
      public enum MyEnumE
      {
          [Description("description of enum1")]
          Enum1,
          [Description("description of enum2")]
          Enum2,
          ...
      }
  
      public MyEnumE MyEnum { get; set; }
  }
  </code></pre>
  
  <p>Here is the <code>Search</code> and <code>SortAndPaginate</code> functions:</p>
  
  <pre><code>public async Task&lt;IPagedList&lt;Item&gt;&gt; Search(ItemCriteria criteria, SortableTypeE sortName, SortOrder.TypeE sortOrder, int pageNb)
      {
          var itemFilter = GenerateFilter(criteria);
          var items = entities.Items.Where(itemFilter);
  
          return await SortAndPaginate(items, sortName, sortOrder, pageNb);
      }
  
      private async Task&lt;IPagedList&lt;Item&gt;&gt; SortAndPaginate(IQueryable&lt;Item&gt; items, SortableTypeE sortName, SortOrder.TypeE sortOrder, int pageNb)
      {
          IOrderedQueryable&lt;Item&gt; result = null;
  
          switch (sortName)
          {
              ...
              case SortableTypeE.Type:
                  result = sortOrder == SortOrder.TypeE.ASC
                      ? items.OrderBy(i =&gt; i.MyEnum.GetDescription())
                      : items.OrderByDescending(i =&gt; i.MyEnum.GetDescription());
                  result = result.ThenBy(i =&gt; i.SomeOtherProperty);
                  break;
              ...
          }
  
          if (result != null)
          {
              return await result.ToPagedListAsync(pageNb, 10);
          }
  
          return PagedListHelper.Empty&lt;Item&gt;();
      }
  </code></pre>
  
  <p>The problem is that the <code>Item</code> table can be quite huge.<br>
  I thought about calling <code>ToListAsync</code> right after <code>entities.Items.Where(itemFilter)</code> but this will get back all filtered items although I only need one page. Does not sound like a good idea.</p>
  
  <p>But if I don't do that <code>EF</code> won't know about <code>GetDescription()</code> mathod and I can only think about two solutions:<br>
  - Change my database column to a string (the enum description) instead of the enum itself (but sounds like a hack to me)<br>
  - Or alphabetically order <code>MyEnumE</code> components directly in the <code>enum</code> declaration (seems dirty and quite unmaintainable too)</p>
  
  <p>I'm quite stuck since I'm concerned about performances if I call <code>ToListAsync</code> right after filtering, all other solutions seem dirty, and I absolutely need a <code>IPagedList</code> returned from the <code>Search</code> method.</p>
  
  <p>Would anyone have an idea about how to deal with this issue ?</p>
  
  <p>Thanks a lot.</p>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>Here is the <code>GetDescription</code> method (can change it if necessary):</p>
  
  <pre><code>public static string GetDescription(this Enum e)
  {
      FieldInfo fi = e.GetType().GetField(e.ToString());
      DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
      if (attributes.Length &gt; 0)
          return attributes[0].Description;
      else
          return e.ToString();
  }
  </code></pre>
  
  <p><strong><em>SOLUTIONS</em></strong></p>
  
  <p>I'll finally go for Ivan Stoev's suggestion because my project is mainly based on <code>Linq</code> (using <code>Linq</code> instead of stored procedures etc.), so this solution seems more suitable to my particular case than creating reference tables.</p>
  
  <p>However <code>Niyoko Yuliawan</code>'s and <code>Michael Freidgeim</code>'s are also really good answers to me, anyone reading this post and having a more database approach should go for their solutions ;)</p>
  
  <p>Thanks a lot to all of you.</p>