讓Linq的OrderBy支援動態欄位,linqorderby
使用linq的OrderBy,如果明確知道是哪個欄位,當然很容易:
IQueryable<User> userQuery = ...; userQuery.OrderBy(u => u.Code)
但假如我們想寫一個通用方法,預先並不知道要用哪個欄位排序呢?
在網上尋尋覓覓,有許多國內的部落格互相抄襲,信誓旦旦,但其實那些代碼都運行不了。
還是老外的好使:
http://www.4byte.cn/question/33782/dynamic-orderby-using-linq-dynamic.html
我拿來修改了一下,可以用。主要思想是擴充Queryable。但裡面的東西有許多我都看不懂。
public static class QueryableExtension { public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName) { return _OrderBy<T>(query, propertyName, false); } public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string propertyName) { return _OrderBy<T>(query, propertyName, true); } static IOrderedQueryable<T> _OrderBy<T>(IQueryable<T> query, string propertyName,bool isDesc) { string methodname = (isDesc) ? "OrderByDescendingInternal" : "OrderByInternal"; var memberProp = typeof(T).GetProperty(propertyName); var method = typeof(QueryableExtension).GetMethod(methodname) .MakeGenericMethod(typeof(T), memberProp.PropertyType); return (IOrderedQueryable<T>)method.Invoke(null, new object[] { query, memberProp }); } public static IOrderedQueryable<T> OrderByInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty) {//public return query.OrderBy(_GetLamba<T, TProp>(memberProperty)); } public static IOrderedQueryable<T> OrderByDescendingInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty) {//public return query.OrderByDescending(_GetLamba<T, TProp>(memberProperty)); } static Expression<Func<T, TProp>> _GetLamba<T, TProp>(PropertyInfo memberProperty) { if (memberProperty.PropertyType != typeof(TProp)) throw new Exception(); var thisArg = Expression.Parameter(typeof(T)); var lamba = Expression.Lambda<Func<T, TProp>>(Expression.Property(thisArg, memberProperty), thisArg); return lamba; } }
調用:
IQueryable<User> userQuery = ...;//正序userQuery = userQuery.OrderBy("Code");//降序userQuery = userQuery.OrderByDescending("Code");
著作權聲明:本文為博主原屙文章,喜歡你就擔走。