title: Better way to accomplish sorting a Generic List of IQueryable?
body: <p>We're using Entity Framework and a "POCO" generator to create our types that get passed around to various layers and they belong to the (changed to protect the innocent) namespace Company.Application.Project.Module.  These POCO objects all inherit from a base class that handles some basic stuff for us.</p>
  
  <p>I would like to write a function that can take a collection of these objects and sort them, by property name.</p>
  
  <p>I have written the following function -- which does the gist of what I want to do, but I don't like it for a few reasons:</p>
  
  <p>1)  This won't work on any object type, it has to be an object type that the SortHelper class is aware of (hence the last using statement).</p>
  
  <p>2)  The "POCO" objects' Type and BaseType seem to be inconsistent -- depending on from where you call this function within the app (unit test project vs. called from a presenter object of our MVP app) which causes problems with the line I've bolded, because if it grabs the wrong Type, the Property won't be on it, in the following line.</p>
  
  <p>From the presenter object, the .GetType is showing up as:
  ClassName_96D74E07A154AE7BDD32624F3B5D38E7F50333608A89B561218F854513E3B746
  ...within the System.Data.Entity.DynamicProxies namespace.</p>
  
  <p>This is why the code says .GetType().BaseType on that line, which gives me:
  ClassName
  ...within Company.Application.Project.Module</p>
  
  <p>But within the unit tests, the .GetType() is showing up as
  ClassName in Company.Application.Project.Module</p>
  
  <p>and BaseType is showing up as
  BaseClass in Company.Application.Project.Module</p>
  
  <p>...which makes more sense, but I don't understand the inconsistency -- and the inconsistency scares me.</p>
  
  <p>3)  Hate using Reflection to do this in general.</p>
  
  <p>If anyone has a better way of doing this, or even a fix to make the Reflection behave with the namespaces/types -- I would certainly appreciate it!</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Reflection;
  using Company.Application.Project.Module;
  
  namespace Company.Application.Project
  {
      public static class SortHelper
      {
          public static IOrderedQueryable&lt;T&gt; Sort&lt;T&gt;(this IQueryable&lt;T&gt; source, string propertyName, bool descending)
          {
  
              // bail out if there's nothing in the list
              if (source == null)
              {
                  return null;
              }
              if (source.Count() == 0)
              {
                  return source as IOrderedQueryable&lt;T&gt;;
              }
  
              // get the type -- or should it be the BaseType?  Nobody knows!
              Type sourceType = source.First().GetType().BaseType;
  
              // this works fine _assuming_ we got the correct type on the line above
              PropertyInfo property = sourceType.GetProperty(propertyName);
              if (descending)
              {
                  return source.OrderByDescending(e =&gt; property.GetValue(e, null));
              }
              else
              {
                  return source.OrderBy(e =&gt; property.GetValue(e, null));
              }
  
          }
      }
  }
  </code></pre>