db.Database.SqlQuery完成分页封装

时间:2021-02-11 16:40:06

调用代码:

            string sql = @"SELECT a.Id ,c.Title,a.Content,a.Status,b.ReportSum FROM dbo.Comment AS a 
                        INNER JOIN(SELECT CommentId, COUNT(Id) AS ReportSum FROM dbo.Reported
                        WHERE Type= 0 AND Status = 1 GROUP BY CommentId) AS b ON a.Id = b.CommentId
                        LEFT JOIN dbo.Consultation AS c ON a.OutsideId = c.Id
                        WHERE a.Type = 0 ";
            List<SqlParameter> parms = new List<SqlParameter>();

            if (!string.IsNullOrEmpty(search))
            {
                sql = sql + " AND c.Title LIKE @Serch ";
                parms.Add(new SqlParameter() { ParameterName = "Serch", Value = "%"+search+ "%" });
            }

            if (isStatus!=3)
            {
                sql = sql + " AND a.Status=@Status ";
                parms.Add(new SqlParameter() { ParameterName = "Status", Value = isStatus });
            }

            result.Total = ServiceSqlHelper.GetPageCount(db, sql, parms);
            var list = ServiceSqlHelper.PagerResult<CommentReported>(db, sql, "Id", parms);

 

封装分页代码:

public static class ServiceSqlHelper
    {

        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbContext">上下文对象</param>
        /// <param name="sql">执行语句</param>
        /// <param name="sort">排序,注意写法</param>
        /// <param name="parms">参数</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns></returns>
        public static List<T> PagerResult<T>(DbContext dbContext, string sql, string sort,List<SqlParameter> parms=null , int pageSize=10, int pageIndex=1)
        {
            int pageStart = pageSize * (pageIndex - 1);
            int pageEnd = pageSize * pageIndex;
            string sqlPage =
                string.Format(@"SELECT * FROM ( 
                SELECT ROW_NUMBER() OVER( ORDER BY {0} DESC) AS RowId, * FROM ({1}) a  
                ) AS t  WHERE t.RowId BETWEEN {2} AND {3}", sort,sql,pageStart,pageEnd) ;

            List<SqlParameter> parms1 = parms == null ? new List<SqlParameter>() : CloneParms(parms);

            var query = dbContext.Database.SqlQuery<T>(sqlPage, parms1.ToArray()).ToList();

            return query;
        }

        /// <summary>
        /// 获取数据总条数
        /// </summary>
        /// <param name="dbContext">上下文对象</param>
        /// <param name="sql">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public static int GetPageCount(DbContext dbContext, string sql, List<SqlParameter> parms=null)
        {
            List<SqlParameter> parms1= parms == null?new List<SqlParameter>(): CloneParms(parms);

            return dbContext.Database.SqlQuery<int>(string.Format("select count(*) from ({0}) a", sql), parms1.ToArray()).FirstOrDefault();
        }

        public static List<SqlParameter> CloneParms(List<SqlParameter> sources)
        {
            List<SqlParameter> list = new List<SqlParameter>();

            foreach (SqlParameter p in sources)
            {
                SqlParameter pp = new SqlParameter() { ParameterName = p.ParameterName, Value = p.Value };
                list.Add(pp);
            }

            return list;
        }
    }