zoukankan      html  css  js  c++  java
  • Linq分页排序通用方法

    1.通用方法     2.调用

    -----------------------------1.-------------------------------------------

    public class CommonRepository<T> where T : class,new()
    {
    /// <summary>
    /// LinQ分页查询
    /// </summary>
    /// <param name="source">条件查询之后的List</param>
    /// <param name="sortExpression">排序字段</param>
    /// <param name="sortDirection">排序方式</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">页数</param>
    /// <param name="totalCount">总数</param>
    /// <returns></returns>
    public IQueryable<T> GetPageListForLinQ(IQueryable<T> source, string sortExpression, string sortDirection, int pageIndex, int pageSize)
    {
    string sortingDir = string.Empty;
    if (sortDirection.ToUpper().Trim() == "ASC")
    {
    sortingDir = "OrderBy";
    }
    else
    {
    sortingDir = "OrderByDescending";
    }
    ParameterExpression param = Expression.Parameter(typeof(T), sortExpression);
    PropertyInfo pi = typeof(T).GetProperty(sortExpression);
    Type[] types = new Type[2];
    types[0] = typeof(T);
    types[1] = pi.PropertyType;
    Expression expr = Expression.Call(typeof(Queryable), sortingDir, types, source.Expression, Expression.Lambda(Expression.Property(param, sortExpression), param));
    IQueryable<T> query = source.AsQueryable().Provider.CreateQuery<T>(expr);
    return query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
    }

    ----------------------------------------2.-------------------------------------------------

    CommonRepository<APIResponsePatientSearchLatelyList> commonRepository = new CommonRepository<APIResponsePatientSearchLatelyList>();

    SqlParameter sqlParametersList = new SqlParameter() {   .....................      };

    string querySql="select * From table where coloms=@a";

    var list = db.Database.SqlQuery<APIResponsePatientSearchLatelyList>(querySql, sqlParametersList);

    IQueryable<APIResponsePatientSearchLatelyList> pageList = commonRepository.GetPageListForLinQ(list.AsQueryable<APIResponsePatientSearchLatelyList>(),
    "pid", ConstHelper.ORDERBY_DESC, pageIndex, pageSize);

  • 相关阅读:
    html表格,table标签
    2-3VRP的基本配置
    6 sys模块
    3 datetime模块
    2 time模块
    1 模块和包的介绍
    12 函数进阶---生成器
    13 函数进阶---迭代器
    10 函数进阶---闭包
    11 函数进阶---装饰器
  • 原文地址:https://www.cnblogs.com/dandan00056/p/11081186.html
Copyright © 2011-2022 走看看