Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the method I use for this:</p> <pre><code>private IQueryable&lt;T&gt; OrderQuery&lt;T&gt;(IQueryable&lt;T&gt; query, OrderParameter orderBy) { string orderMethodName = orderBy.Direction == SortDirection.Ascending ? "OrderBy" : "OrderByDescending"; Type t = typeof(T); var param = Expression.Parameter(t, "shipment"); var property = t.GetProperty(orderBy.Attribute); /* We can't just call OrderBy[Descending] with an Expression * parameter because the second type argument to OrderBy is not * known at compile-time. */ return query.Provider.CreateQuery&lt;T&gt;( Expression.Call( typeof(Queryable), orderMethodName, new Type[] { t, property.PropertyType }, query.Expression, Expression.Quote( Expression.Lambda( Expression.Property(param, property), param)) )); } </code></pre> <p><code>OrderParameter</code> is just a struct with an attribute and direction.</p> <p>EDIT: Additional explanation.</p> <p>This method is from my <code>DynamicOrderList</code> class, which is a list of <code>OrderParameter</code> objects. If all you need is sorting by one field, then you can simplify it a bit:</p> <pre><code>private IQueryable&lt;T&gt; OrderByDynamic&lt;T&gt;(this IQueryable&lt;T&gt; query, string attribute, SortDirection direction) { try { string orderMethodName = direction == SortDirection.Ascending ? "OrderBy" : "OrderByDescending"; Type t = typeof(T); var param = Expression.Parameter(t); var property = t.GetProperty(attribute); return query.Provider.CreateQuery&lt;T&gt;( Expression.Call( typeof(Queryable), orderMethodName, new Type[] { t, property.PropertyType }, query.Expression, Expression.Quote( Expression.Lambda( Expression.Property(param, property), param)) )); } catch (Exception) // Probably invalid input, you can catch specifics if you want { return query; // Return unsorted query } } </code></pre> <p>Then use it like:</p> <pre><code>myQuery = myQuery.OrderByDynamic("name", SortDirection.Ascending); </code></pre> <p>EDIT 2:</p> <pre><code>public IQueryable&lt;T&gt; OrderBy&lt;T&gt;(this IQueryable&lt;T&gt; query, string attribute, SortDirection direction) { return ApplyOrdering(query, attribute, direction, "OrderBy"); } public IQueryable&lt;T&gt; ThenBy&lt;T&gt;(this IQueryable&lt;T&gt; query, string attribute, SortDirection direction) { return ApplyOrdering(query, attribute, direction, "ThenBy"); } private IQueryable&lt;T&gt; ApplyOrdering&lt;T&gt;(IQueryable&lt;T&gt; query, string attribute, SortDirection direction, string orderMethodName) { try { if (direction == SortDirection.Descending) orderMethodName += "Descending"; Type t = typeof(T); var param = Expression.Parameter(t); var property = t.GetProperty(attribute); return query.Provider.CreateQuery&lt;T&gt;( Expression.Call( typeof(Queryable), orderMethodName, new Type[] { t, property.PropertyType }, query.Expression, Expression.Quote( Expression.Lambda( Expression.Property(param, property), param)) )); } catch (Exception) // Probably invalid input, you can catch specifics if you want { return query; // Return unsorted query } } </code></pre> <p>And:</p> <pre><code>myQuery=myQuery.OrderBy("name", SortDirection.Ascending).ThenBy("date", SortDirection.Descending); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload