zoukankan      html  css  js  c++  java
  • ef 分页

       public List<TEntity> FindList(Expression<Func<TEntity, bool>> predicate, Pagination pagination)
            {
                MovieSiteEntities db = new MovieSiteEntities();
                pagination.records = db.Set<TEntity>().Where(predicate).Count();  
    
                bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
                string[] _order = pagination.sidx.Split(',');
                MethodCallExpression resultExp = null;
                var tempData = dbcontext.Set<TEntity>().Where(predicate);
                foreach (string item in _order)
                {
                    string _orderPart = item;
                    _orderPart = Regex.Replace(_orderPart, @"s+", " ");
                    string[] _orderArry = _orderPart.Split(' ');
                    string _orderField = _orderArry[0];
                    bool sort = isAsc;
                    if (_orderArry.Length == 2)
                    {
                        isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                    }
                    var parameter = Expression.Parameter(typeof(TEntity), "t");
                    var property = typeof(TEntity).GetProperty(_orderField);
                    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                    var orderByExp = Expression.Lambda(propertyAccess, parameter);
                    resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
                }
    
                tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
                //pagination.records = tempData.Count();
                tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
                return tempData.ToList();
            }
    

      

  • 相关阅读:
    略少面试题 项目中用到的技术详解 有用
    python在VM+centos7 下面的安装
    shell基础09 归档数据
    shell基础10 sed,gawk和shell的对比
    shell练习03 mysql在脚本中的使用
    shell练习03 安装mysql
    shell基础09 gawk程序(上)
    shell练习02 归档数据文件
    shell基础08 sed命令行编辑器(上)
    shell基础07 函数
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/6590121.html
Copyright © 2011-2022 走看看