Пример #1
0
        public static PagedResult <T> PagedQuery <T>(this IDbConnection db, int pageSize, int pageNumber,
                                                     Expression <Func <T, bool> > whereExpression = null, Expression <Func <T, object> > groupByexpression = null,
                                                     IDbTransaction trans = null, int?commandTimeout = null,
                                                     Expression <Func <T, object> > orderbyExpression = null)
            where T : class
        {
            var sqllam     = new SqlExp <T>(db.GetAdapter());
            var countSqlam = new SqlExp <T>(db.GetAdapter());

            if (whereExpression != null)
            {
                sqllam     = sqllam.Where(whereExpression);
                countSqlam = countSqlam.Where(whereExpression);
            }

            if (orderbyExpression != null)
            {
                sqllam = sqllam.OrderBy(orderbyExpression);
            }

            if (groupByexpression != null)
            {
                sqllam = sqllam.GroupBy(groupByexpression);
            }

            countSqlam = countSqlam.Count();

            int countRet;
            var sqlString = countSqlam.SqlString;

            try {
                DebuggingSqlString(sqlString);
                countRet = db.Query <int>(sqlString, countSqlam.Parameters).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }
            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                DebuggingSqlString(sqlstring);
                var retlist = db.Query <T>(sqlstring, sqllam.Parameters, trans, commandTimeout: commandTimeout);

                return(new PagedResult <T>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }