1  /// <summary>
 2         /// 根据指定属性名称对序列进行排序
 3         /// </summary>
 4         /// <typeparam name="TSource">source中的元素的类型</typeparam>
 5         /// <param name="source">一个要排序的值序列</param>
 6         /// <param name="property">属性名称</param>
 7         /// <param name="descending">是否降序</param>
 8         /// <returns></returns>
 9         public static IQueryable<TSource> OrderBy<TSource>(this IQueryable<TSource> source, string property, bool descending) where TSource : class
10         {
11             ParameterExpression param = Expression.Parameter(typeof(TSource), "c");
12             PropertyInfo pi = typeof(TSource).GetProperty(property);
13             MemberExpression selector = Expression.MakeMemberAccess(param, pi);
14             LambdaExpression le = Expression.Lambda(selector, param);
15             string methodName = (descending) ? "OrderByDescending" : "OrderBy";
16             MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TSource), pi.PropertyType }, source.Expression, le);
17             return source.Provider.CreateQuery<TSource>(resultExp);

 

 items = SortLinQUtil.OrderBy(items, param.orderName, true);

相关文章:

  • 2021-07-20
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-20
  • 2021-10-03
  • 2022-12-23
  • 2022-01-21
  • 2021-07-08
相关资源
相似解决方案