title: How to incorporate ASP.NET Web API ODATA into ActiveRecord/NHibernate
body: <p>In ASP.NET Web API, it allows you to write ODATA queries in the url string to specify which data you want to return from a method. However the part that I'm having a hard time grasping is that ODATA works to filter an IQueryable collection of C# objects, not the database table itself. </p>
  
  <p>This is impractical because really you would want to filter at the database level, as it would be horrible to return all objects from the database, load them into a C# IQueryable list, and then have the ODATA filter that list. </p>
  
  <p>Here is the code, which uses NHibernate and Castle Active Record for data access:</p>
  
  <pre><code>public IQueryable&lt;Message&gt; GetAll()
  {
      return from m in MessageData.FindAllQueryable()
             select ConvertToView(m);
  }
  
  public static IQueryable&lt;Message&gt; FindAllQueryable()
  {
      var criteria = DetachedCriteria.For&lt;Message&gt;()
          .CreateAlias("MessageRecipients", "mr")
          .AddOrder(new Order("Id", false));
      return ActiveRecordMediator&lt;Message&gt;.FindAll(criteria).AsQueryable();
  }
  </code></pre>
  
  <p>The end result of this code would be it returning all messages from the database. How do I allow the ODATA to perform its filters upon the database itself? Otherwise this whole concept of ODATA is completely impractical for real world situations.</p>