本系列目錄:
使用EF構建企業級應用(一):主要講數據庫訪問基類IRepository及Repository 的實現
使用EF構建企業級應用(二):主要講動態排序擴展的實現
使用EF構建企業級應用(三):主要講靈活的構建查詢條件表達式Expression<Func<TEntity,bool>>.
使用EF構建企業級應用(四):主要講下在MVC環境中前端開發中如何郵箱的使用,及一個實例源碼包
動態排序擴展
在上一節(使用EF構建企業級應用(一) ) 中,我們實現了數據庫基本操作的CURD的定義,如果你直接復制這個代碼到VS中編譯,奇怪的問題就出現了,可能會出好幾個錯誤,錯誤發生在類似這樣的代碼上”IQueryable<TEntity> query = Get(expression).OrderBy(orderPropertyName, isAscending);”,大致的錯誤可能是,

這是為啥呢? 該不會是樓主忽悠吧,這個自然不會,且聽如下分解.
我們常使用的排序可能是如下樣子:var temp=lst.OrderBy(t=>t.Code).ToList();因為后端我們並不清楚在數據庫查詢的時候是對什么字段排序,那么我們如何動態來構建這個類似於OrderBy中的(t=>t.Code)表達式呢?
在定義中”public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector) ”,
/// <summary>
/// 動態排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="propertyName">按T類型中排序屬性名</param>
/// <param name="isAscending">是否是升序排序</param>
/// <returns></returns>
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName, bool isAscending) where T : class
{
var type = typeof(T);
Expression resultExp = null;
var property = type.GetProperty(propertyName);
if (property == null)
throw new ArgumentException("propertyName", "不存在");
var param = Expression.Parameter(type, "p");
Expression propertyAccessExpression = Expression.MakeMemberAccess(param, property);
var orderByExpression = Expression.Lambda(propertyAccessExpression, param);
var methodName = isAscending ? "OrderBy" : "OrderByDescending";
resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(resultExp);
}
上面方法終於解決動態排序的問題,直到有一天,業務上在查詢產品信息的時候,需要按照產品大類名稱排序,我們很自然的寫成了var lst=lst.OrderBy(“ProductCategory.Name”,true)這樣的調用,結果卻出現了類似於
這樣的錯誤,大致意思就是當前屬性在指定的類型中不存在,很顯然這樣的寫法只支持按實體簡單的屬性排序,沒有辦法做關聯排序,為讓大家更明白一點,把產品和產品大類定義描述一下
/// <summary>
/// 產品信息
/// </summary>
public class Product
{
public Guid Id{get;set;}
public string Name{get;set;}
//產品大類
public ProductCategory ProductCategory{get;set;}
}
public class ProductCategory
{
public Guid Id{get;set;}
public string Name{get;set;}
}
想必大家明白了問題的所在,我們是需要構建一個按 Product下面的ProductCategory 對象中的 Name字段排序,在有了上面的經驗之后,經過不斷的goodle+單步調試,我們對排序擴展有了如下的改進
var methodName = isAscending ? "OrderBy" : "OrderByDescending";
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
下面貼一個完整版的排序解決方案,包含一個對IEnumerable<T>的排序擴展
/// <summary>
/// 排序類型
/// </summary>
public enum EOrderType
{
OrderBy = 0,
OrderByDescending = 1,
ThenBy = 2,
ThenByDescending = 3
}
/// <summary>
/// 排序擴展
/// </summary>
public static class OrderEx
{
/// <summary>
/// 動態排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="propertyName"></param>
/// <param name="isAscending"></param>
/// <returns></returns>
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName, bool isAscending) where T : class
{
//有關聯屬性
if (propertyName.IndexOf('.') > 0)
{
if (isAscending)
return source.OrderBy(propertyName);
else
return source.OrderByDescending(propertyName);
}
//簡單屬性
else
{
var type = typeof(T);
Expression resultExp = null;
var property = type.GetProperty(propertyName);
if (property == null)
throw new ArgumentException("propertyName", "不存在");
var param = Expression.Parameter(type, "p");
Expression propertyAccessExpression = Expression.MakeMemberAccess(param, property);
var orderByExpression = Expression.Lambda(propertyAccessExpression, param);
var methodName = isAscending ? "OrderBy" : "OrderByDescending";
resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { type, property.PropertyType },
source.Expression, Expression.Quote(orderByExpression));
return source.Provider.CreateQuery<T>(resultExp);
}
}
/// <summary>
/// 升序排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="property"></param>
/// <returns></returns>
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, EOrderType.OrderBy);
}
/// <summary>
/// 降序排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="property"></param>
/// <returns></returns>
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, EOrderType.OrderByDescending);
}
/// <summary>
/// ThenBy
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="property"></param>
/// <returns></returns>
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, EOrderType.ThenBy);
}
/// <summary>
/// ThenByDescending
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="property"></param>
/// <returns></returns>
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, EOrderType.ThenByDescending);
}
/// <summary>
/// 應用排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="property"></param>
/// <param name="methodName"></param>
/// <returns></returns>
public static IOrderedQueryable<T> ApplyOrder<T>(this IQueryable<T> source, string property, EOrderType orderType)
{
var methodName = orderType.ToString();
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
/// <summary>
/// 動態排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="propertyName"></param>
/// <param name="isAscSort"></param>
/// <returns></returns>
public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> source, string propertyName, bool isAscSort) where T : class
{
return ApplyOrder(source, propertyName, isAscSort ? EOrderType.OrderBy : EOrderType.OrderByDescending);
}
/// <summary>
/// 處理排序
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="property"></param>
/// <param name="methodName"></param>
/// <returns></returns>
public static IOrderedEnumerable<T> ApplyOrder<T>(IEnumerable<T> source, string property, EOrderType orderType)
{
var methodName = orderType.ToString();
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
var orderMethod = typeof(Enumerable).GetMethods().Single(method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2);
object result = orderMethod
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda.Compile() });
return (IOrderedEnumerable<T>)result;
}
}
在第一篇文章中,細心的同學們可能會發現,我們在查詢的時候,查詢條件參數寫成了Expression<Func<TEntity, bool>> expression這樣的參數形式,那么我們如何動態的來構建這個查詢表達式呢?那我們就在下一章節中來討論下這個問題.
