title: Dynamiclinq library with generic repository
body: <p>Does anyone have any examples of using the dynamic linq library with a generic repository pattern?</p>
  
  <p><a href="http://nuget.org/packages/DynamicLINQ" rel="nofollow">http://nuget.org/packages/DynamicLINQ</a></p>
  
  <p><a href="http://weblogs.asp.net/davidfowler/archive/2010/08/19/dynamic-linq-part-2-evolution.aspx" rel="nofollow">http://weblogs.asp.net/davidfowler/archive/2010/08/19/dynamic-linq-part-2-evolution.aspx</a></p>
  
  <p>The trouble I'm finding is that dynamicorderby() returns IQueryable, so I can't really use it in a generic repository. And it's cool overriding of orderby e.g. orderby(o => o["mycolumn"]) I can only seem to get working on a concrete implementation.</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>Here's some sample code, rather than moving it up the stack into the repository, which I'm trying to avoid as the repositories are really clean at the moment, I'm putting it into the business layer (which is ok for the moment).</p>
  
  <pre><code>    public IEnumerable&lt;BusinessUnitUser&gt; GetUsers(int pageSize, int pageNumber, out int totalPages, Func&lt;dynamic, dynamic&gt; orderBy, Expression&lt;Func&lt;BusinessUnitUser, bool&gt;&gt; where = null)
      {
          IQueryable&lt;dynamic&gt; dynamicQuery = buUserRepository.Get(pageSize, pageNumber, out totalPages).DynamicOrderBy(orderBy);
  
          ...
       }
  </code></pre>
  
  <p>And calling it with sometihng like:</p>
  
  <pre><code>userBL.GetUsers(gvUser.PageSize, gvUser.PageIndex, out recordCount, o  =&gt; o["UserID"])
  </code></pre>
  
  <p>I've put ... as I haven't figuired out how to cast it to IEnumerable yet, but that's not really the problem yet.</p>
  
  <p>The main problem is tat I'm getting an error</p>
  
  <blockquote>
    <p>Unable to cast object of type
    'System.Data.Objects.ObjectQuery<code>1[BusinessUnitUser]'
    to type 'System.Data.Objects.ObjectQuery</code>1[System.Object]'.</p>
  </blockquote>
  
  <p>Which is caused on the .DynamicOrderBy(orderBy); </p>
  
  <hr>
  
  <p>EDIT: Here is the stack trace</p>
  
  <blockquote>
    <p>[InvalidCastException: Unable to cast object of type
    'System.Data.Objects.ObjectQuery<code>1[EntityLayer.BusinessUnit]'
    to type 'System.Data.Objects.ObjectQuery</code>1[System.Object]'.]<br>
    System.Data.Entity.Internal.Linq.InternalQuery<code>1..ctor(InternalContext
    internalContext, ObjectQuery objectQuery) +170<br>
    System.Data.Entity.Internal.Linq.DbQueryProvider.CreateQuery(Expression
    expression) +116    DynamicLINQ.DynamicQueryable.OrderBy(IQueryable</code>1
    source, LambdaExpression expression) +356<br>
    DynamicLINQ.DynamicQueryable.DynamicOrderBy(IQueryable<code>1 source,
    Func</code>2 selector) +58<br>
    BusinessLayer.BusinessUnitBL.Get(Int32 pageSize, Int32
    pageNumber, Int32&amp; totalPages, Func<code>2 orderBy, Expression</code>1 where) in
    C:\TEST\BusinessUnitBL.cs:23<br>
    UserInterface.Pages.Multitenant.BusinessUnit.DataBindBusinessUnits()
    in C:\TEST\BusinessUnit.aspx.cs:159<br>
    UserInterface.Pages.Multitenant.BusinessUnit.SetGridPagerSettings()
    in C:\TEST\BusinessUnit.aspx.cs:177<br>
    UserInterface.Pages.Multitenant.BusinessUnit.OnLoad(EventArgs
    e) in C:\TEST\BusinessUnit.aspx.cs:49<br>
    System.Web.UI.Control.LoadRecursive() +74<br>
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +2207</p>
  </blockquote>