1.动态构建排序 Lambda
/// <summary> /// 获取排序Lambda(如果动态排序,类型不同会导致转换失败) /// </summary> /// <typeparam name="T">数据字段类型</typeparam> /// <typeparam name="Tkey">排序字段类型</typeparam> /// <param name="defaultSort">默认的排序字段</param> /// <param name="sort">当前排序字段</param> /// <returns></returns> public static Expression<Func<T, Tkey>> SortLambda<T, Tkey>(string defaultSort, string sort) { //1.创建表达式参数(指定参数或变量的类型:p) var param = Expression.Parameter(typeof(T), "t"); //2.构建表达式体(类型包含指定的属性:p.Name) var body = Expression.Property(param, string.IsNullOrEmpty(sort) ? defaultSort : sort); //3.根据参数和表达式体构造一个lambda表达式 return Expression.Lambda<Func<T, Tkey>>(Expression.Convert(body, typeof(Tkey)), param); }
使用方法:
public IQueryable<T> GetModelsByPage<Tkey>(int pageSize, int pageIndex, bool isAsc, Expression<Func<T, Tkey>> orderByLambda, Expression<Func<T, bool>> whereLambda, out int total) { total = dbContext.Set<T>().Where(whereLambda).Count(); //是否升序 if (isAsc) { return dbContext.Set<T>().Where(whereLambda).OrderBy(orderByLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize); } else { return dbContext.Set<T>().Where(whereLambda).OrderByDescending(orderByLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize); } }
缺点:Tkey必须限定,但不一定知道字段类型,错误的类型会导致转换失败,切返回值不能固定为object,如有其他方法,希望大牛给出建议。
2.对ef的OrderBy方法进行扩展(此方法无需指定类型)
/// <summary> /// 查询扩展方法 /// </summary> 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, System.Reflection.PropertyInfo memberProperty) {//public return query.OrderBy(_GetLamba<T, TProp>(memberProperty)); } public static IOrderedQueryable<T> OrderByDescendingInternal<T, TProp>(IQueryable<T> query, System.Reflection.PropertyInfo memberProperty) {//public return query.OrderByDescending(_GetLamba<T, TProp>(memberProperty)); } static Expression<Func<T, TProp>> _GetLamba<T, TProp>(System.Reflection.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; } }
使用方法:
public IQueryable<T> GetModelsByPage(int pageSize, int pageIndex, bool isAsc, string orderByField, Expression<Func<T, bool>> whereLambda, out int total) { total = dbContext.Set<T>().Where(whereLambda).Count(); //是否升序 if (isAsc) { return dbContext.Set<T>().Where(whereLambda).OrderBy(orderByField).Skip((pageIndex - 1) * pageSize).Take(pageSize); } else { return dbContext.Set<T>().Where(whereLambda).OrderByDescending(orderByField).Skip((pageIndex - 1) * pageSize).Take(pageSize); } }
缺点:朕看不懂!