Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have find the way. </p> <p><a href="http://www.codeproject.com/KB/linq/IEnumerableSortByName.aspx?msg=3005452#xx3005452xx" rel="nofollow noreferrer">http://www.codeproject.com/KB/linq/IEnumerableSortByName.aspx?msg=3005452#xx3005452xx</a></p> <p>we can use the extend method SortEngine</p> <pre><code>private static IOrderedEnumerable&lt;T&gt; SortEngine&lt;T&gt;(this IEnumerable&lt;T&gt; source, string columnName, bool isAscending, bool started) { var item = Expression.Parameter(typeof(T), "item"); var propertyValue = Expression.PropertyOrField(item, columnName); var propertyLambda = Expression.Lambda(propertyValue, item); // item =&gt; item.{columnName} var sourceExpression = Expression.Parameter(typeof(IEnumerable&lt;T&gt;), "source"); string methodName; Expression inputExpression; if (started) { methodName = isAscending ? "ThenBy" : "ThenByDescending"; inputExpression = Expression.Convert(sourceExpression, typeof(IOrderedEnumerable&lt;T&gt;)); // ThenBy requires input to be IOrderedEnumerable&lt;T&gt; } else { methodName = isAscending ? "OrderBy" : "OrderByDescending"; inputExpression = sourceExpression; } var sortTypeParameters = new Type[] { typeof(T), propertyValue.Type }; var sortExpression = Expression.Call(typeof(Enumerable), methodName, sortTypeParameters, inputExpression, propertyLambda); var sortLambda = Expression.Lambda&lt;Func&lt;IEnumerable&lt;T&gt;, IOrderedEnumerable&lt;T&gt;&gt;&gt;(sortExpression, sourceExpression); // source =&gt; Enumerable.OrderBy&lt;T, TKey&gt;(source, item =&gt; item.{columnName}) return sortLambda.Compile()(source); } public static IOrderedEnumerable&lt;T&gt; OrderBy&lt;T&gt;(this IEnumerable&lt;T&gt; source, string columnName) { return SortEngine(source, columnName, true, false); } public static IOrderedEnumerable&lt;T&gt; OrderByDescending&lt;T&gt;(this IEnumerable&lt;T&gt; source, string columnName) { return SortEngine(source, columnName, false, false); } public static IOrderedEnumerable&lt;T&gt; ThenBy&lt;T&gt;(this IOrderedEnumerable&lt;T&gt; source, string columnName) { return SortEngine(source, columnName, true, true); } public static IOrderedEnumerable&lt;T&gt; ThenByDescending&lt;T&gt;(this IOrderedEnumerable&lt;T&gt; source, string columnName) { return SortEngine(source, columnName, false, true); } </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. 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