Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It the underlying type doesn't <strong>have</strong> an obvious <code>Name</code> member, this I can't see how this would work.</p> <p>If the problem is simpy that you only know the column to order by at runtime; then to order by a dynamic property you need to build an <code>Expression</code> on the fly. Here's some old code I have that does this, and should support "Name" and things like "Customer.Name" (child properties); I haven't tested it recently, though:</p> <pre><code>public static class OrderExtensions { public static IOrderedQueryable&lt;T&gt; OrderBy&lt;T&gt;(this IQueryable&lt;T&gt; source, string property) { return ApplyOrder&lt;T&gt;(source, property, "OrderBy"); } public static IOrderedQueryable&lt;T&gt; OrderByDescending&lt;T&gt;(this IQueryable&lt;T&gt; source, string property) { return ApplyOrder&lt;T&gt;(source, property, "OrderByDescending"); } public static IOrderedQueryable&lt;T&gt; ThenBy&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source, string property) { return ApplyOrder&lt;T&gt;(source, property, "ThenBy"); } public static IOrderedQueryable&lt;T&gt; ThenByDescending&lt;T&gt;(this IOrderedQueryable&lt;T&gt; source, string property) { return ApplyOrder&lt;T&gt;(source, property, "ThenByDescending"); } static IOrderedQueryable&lt;T&gt; ApplyOrder&lt;T&gt;(IQueryable&lt;T&gt; source, string property, string methodName) { ParameterExpression arg = Expression.Parameter(typeof(T), "x"); Expression expr = arg; foreach(string prop in property.Split('.')) { // use reflection (not ComponentModel) to mirror LINQ expr = Expression.PropertyOrField(expr, prop); } Type delegateType = typeof(Func&lt;,&gt;).MakeGenericType(typeof(T), expr.Type); LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); return (IOrderedQueryable&lt;T&gt;) typeof(Queryable).GetMethods().Single( method =&gt; method.Name == methodName &amp;&amp; method.IsGenericMethodDefinition &amp;&amp; method.GetGenericArguments( ).Length ==2 &amp;&amp; method.GetParameters().Length == 2) .MakeGenericMethod(typeof(T), expr.Type) .Invoke(null, new object[] {source, lambda}); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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