• Entity Framework 4、5 多字段排序


    public interface IOrderByExpression<TEntity> where TEntity : class
    {
        IOrderedQueryable<TEntity> ApplyOrderBy(IQueryable<TEntity> query);
        IOrderedQueryable<TEntity> ApplyThenBy(IOrderedQueryable<TEntity> query);
    }

    public class OrderByExpression<TEntity, TOrderBy> : IOrderByExpression<TEntity>
        where TEntity : class
    {
        private Expression<Func<TEntity, TOrderBy>> _expression;
        private bool _descending;
    
        public OrderByExpression(Expression<Func<TEntity, TOrderBy>> expression,
            bool descending = false)
        {
            _expression = expression;
            _descending = descending;
        }
    
        public IOrderedQueryable<TEntity> ApplyOrderBy(
            IQueryable<TEntity> query)
        {
            if (_descending)
                return query.OrderByDescending(_expression);
            else
                return query.OrderBy(_expression);
        }
    
        public IOrderedQueryable<TEntity> ApplyThenBy(
            IOrderedQueryable<TEntity> query)
        {
            if (_descending)
                return query.ThenByDescending(_expression);
            else
                return query.ThenBy(_expression);
        }
    }


     

     调用方法

    public static class IQueryableHelper
    {
            /// <summary>
            /// IQueryable 接口的多字段排序 请在执行底层时使用
            /// </summary>
            /// <typeparam name="TEntity">目标类型</typeparam>
            /// <param name="query"></param>
            /// <param name="orderByExpressions"></param>
            /// <returns></returns>
            public static IQueryable<TEntity> ApplyOrderBy<TEntity>(this IQueryable<TEntity> query,
       params IOrderByExpression<TEntity>[] orderByExpressions)
       where TEntity : class
            {
                if (orderByExpressions == null)
                    return query;
                IOrderedQueryable<TEntity> output = null;
    
                foreach (var orderByExpression in orderByExpressions)
                {
                    if (output == null)//检查是否是第一次
                        output = orderByExpression.ApplyOrderBy(query);
                    else
                        output = orderByExpression.ApplyThenBy(output);
                }
    
                return output ?? query;
            }
    }

    调用处

     using (var db = ConnectionHelper.GetConn())
                {
                    var list = db.tb_Store.ApplyOrderBy(new OrderByExpression<tb_Store, string>(u => u.StorePhone),
                          new OrderByExpression<tb_Store, string>(u => u.StoreCode, true)
                          ).Skip(skip).Take(take);//用自己写的扩展方法代替原来的orderby 
                    var xx = ((ObjectQuery)list).ToTraceString(); //这里可以看一下生成的sql语句 
                }
  • 相关阅读:
    spring小结2:spring管理bean原理(转帖)
    JAVA Web.xml 加载顺序
    mysql into outfile导出方式
    从平安面试归来
    如何避开求职陷阱
    关注基金走势先~~
    如何谈薪资
    包含数据分析的坐标图测试
    面试题分享
    ShaiShai.net
  • 原文地址:https://www.cnblogs.com/xingbo/p/4377909.html
Copyright © 2020-2023  润新知