• Lambda动态排序分页通用方法


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;
    
    namespace Common
    {
        public class QueryParameters
        {
            public QueryParameters();
    
            public string order { get; set; }
            public int page { get; set; }
            public int rows { get; set; }
            public string sort { get; set; }
        }
    
        public class DataGrid<T>
        {
            /// <summary>
            /// 默认构造函数
            /// </summary>
            public DataGrid() { }
    
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="rows">表格数据</param>
            public DataGrid(List<T> rows)
            {
                this.rows = rows;
            }
            private List<T> _rows = new List<T>();
    
            /// <summary>
            /// 表格数据
            /// </summary>
            public List<T> rows
            {
                get { return _rows; }
                set
                {
                    if (value != null)
                    {
                        _rows = value;
                    }
                }
            }
    
            /// <summary>
            /// 总记录数
            /// </summary>
            public int total { get; set; }
    
            public static DataGrid<T> QueryWithParameters(IEnumerable<T> queryable, QueryParameters queryParameters)
            {
                if (queryable == null)
                {
                    return new DataGrid<T>();
                }
                queryParameters = queryParameters ?? new QueryParameters();
                IQueryable<T> source = Queryable.AsQueryable<T>(queryable);
    
                var page = queryParameters.page;
                var rows = queryParameters.rows;
                var sort = queryParameters.sort;
                var order = queryParameters.order;
    
                int num = source.Count();
    
                int count = (page - 1) * rows;
    
                IQueryable<T> s = source;
    
                if (!string.IsNullOrWhiteSpace(sort))
                {
                    var sortExpression = Expression.Parameter(source.ElementType);
                    var selector = Expression.Lambda(Expression.PropertyOrField(sortExpression, sort), sortExpression);
                    if (order.ToLower() == "asc")
                    {
                        s = (IQueryable<T>)source.Provider.CreateQuery(Expression.Call(typeof(Queryable), "OrderBy", new Type[] { source.ElementType, selector.Body.Type }, source.Expression, selector));
                    }
                    else if (order.ToLower() == "desc")
                    {
                        s = (IQueryable<T>)source.Provider.CreateQuery(Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { source.ElementType, selector.Body.Type }, source.Expression, selector));
                    }
                }
    
                //方法1
                s = count == 0 ? s.Take(rows) : s.Skip(count).Take(rows);
                //方法2
                s = count == 0 ? Queryable.Take<T>(s, rows) : Queryable.Take<T>(Queryable.Skip<T>(s, count), rows);
    
                return new DataGrid<T>()
                {
                    total = num,
                    rows = s.ToList()
                };
            }
        }
    }

    调用:

    DataGrid<T>.QueryWithParameters(query, paging)
  • 相关阅读:
    字节跳动_玩转客户端训练营
    Mac更换鼠标指针样式_mousecape教程
    能否使用GHDL+GTKWave代替Quartus ii
    关于新家布置的一点小经验——开博第一篇
    Azure直播活动资源转成MP4并下载到本地
    程序员必备的6款工具软件!
    Java 类型的 Hibernate 映射
    Window10下Erlang和RabbitMQ的下载安装
    Tomcat部署项目,JS文件及后台数据显示都是乱码问题解决
    go语言入门及基本算法
  • 原文地址:https://www.cnblogs.com/zhuyongblogs/p/5972221.html
Copyright © 2020-2023  润新知