Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web.UI.WebControls; using dynamic = System.Linq.Dynamic; using System.Linq.Expressions; namespace Lib.Extensions { public static class Utils { /// &lt;summary&gt; /// Sorts by Model property with dynamic expression /// &lt;/summary&gt; /// &lt;param name="list"&gt; &lt;/param&gt; /// &lt;param name="propertyName"&gt;&lt;/param&gt; /// &lt;param name="sortDirection"&gt; &lt;/param&gt; public static List&lt;TObject&gt; SortByModelProperty&lt;TObject, TProperty&gt;(this List&lt;TObject&gt; list, Expression&lt;Func&lt;TObject, TProperty&gt;&gt; property, SortDirection sortDirection) { string propertyName = GetPropertyName(property); string exp1 = string.Format("model.{0}", propertyName); var p1 = Expression.Parameter(typeof(TObject), "model"); var e1 = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p1 }, null, exp1); if (e1 != null) { if (sortDirection == SortDirection.Ascending) { var result = list.OrderBy((Func&lt;TObject, TProperty&gt;)e1.Compile()).ToList(); return result; } else { var result = list.OrderByDescending((Func&lt;TObject, TProperty&gt;)e1.Compile()).ToList(); return result; } } return list; } private static string GetPropertyName&lt;TObject, TProperty&gt;(Expression&lt;Func&lt;TObject, TProperty&gt;&gt; property) { MemberExpression memberExpression = (MemberExpression)property.Body; PropertyInfo propertyInfo = (PropertyInfo)memberExpression.Member; return propertyInfo.Name; } } } </code></pre> <p>You should add System.Linq.Dynamic.You can find it Nuget package manager. I think that This extension method helps you.</p>
    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. 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