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
    {
        /// 
        /// 默认构造函数
        /// 
        public DataGrid() { }

        /// 
        /// 构造函数
        /// 
        /// 表格数据
        public DataGrid(List rows)
        {
            this.rows = rows;
        }
        private List _rows = new List();

        /// 
        /// 表格数据
        /// 
        public List rows
        {
            get { return _rows; }
            set
            {
                if (value != null)
                {
                    _rows = value;
                }
            }
        }

        /// 
        /// 总记录数
        /// 
        public int total { get; set; }

        public static DataGrid QueryWithParameters(IEnumerable queryable, QueryParameters queryParameters)
        {
            if (queryable == null)
            {
                return new DataGrid();
            }
            queryParameters = queryParameters ?? new QueryParameters();
            IQueryable source = Queryable.AsQueryable(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 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)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)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(s, rows) : Queryable.Take(Queryable.Skip(s, count), rows);

            return new DataGrid()
            {
                total = num,
                rows = s.ToList()
            };
        }
    }
}

调用:

DataGrid.QueryWithParameters(query, paging)

 

转载于:https://www.cnblogs.com/zhuyongblogs/p/5972221.html

你可能感兴趣的:(Lambda动态排序分页通用方法)