Пример #1
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            Expression result = base.VisitMethodCall(node);

            if (node.Method.Name == nameof(Enumerable.OrderBy) || node.Method.Name == nameof(Enumerable.ThenBy))
            {
                _sqlQuery.OrderBy(ChangeType((LambdaExpression)node.Arguments.OfType <UnaryExpression>().ExtendedSingle($"Finding Operand in {node.Method.Name}").Operand));
            }

            if (node.Method.Name == nameof(Enumerable.OrderByDescending) || node.Method.Name == nameof(Enumerable.ThenByDescending))
            {
                _sqlQuery.OrderByDescending(ChangeType((LambdaExpression)node.Arguments.OfType <UnaryExpression>().ExtendedSingle($"Finding Operand in {node.Method.Name}").Operand));
            }

            return(result);
        }
Пример #2
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, params Expression <Func <T, object> >[] orderbyExpressions)
            where T : class
        {
            var sqllam     = new SqlLam <T>(db.GetAdapter());
            var countSqlam = new SqlLam <T>(db.GetAdapter());

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

            if (orderbyExpressions != null && orderbyExpressions.Length > 0)
            {
                sqllam = sqllam.OrderBy(orderbyExpressions);
            }

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

            countSqlam = countSqlam.Count();

            var countRet = db.Query <int>(countSqlam.SqlString, countSqlam.Parameters).FirstOrDefault();

            //var sqlString = sqllam.SqlString;
            //var param = sqllam.Parameters;
            //string parameterString = GetParameterString(sqllam.Parameters);

            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            var retlist = db.Query <T>(sqlstring, sqllam.Parameters);

            return(new PagedResult <T>(retlist, countRet, pageSize, pageNumber));
        }
Пример #3
0
        /// <summary>
        /// Return posts based on pagination and sorting parameters.
        /// </summary>
        /// <param name="pageNo">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="sortColumn">Sort column name</param>
        /// <param name="sortByAscending">True to sort by ascending</param>
        /// <returns></returns>
        public IList<Post> Posts(int pageNo, int pageSize, string sortColumn, bool sortByAscending)
        {
            SqlLamBase.SetAdapter(SqlAdapter.SqlServer2008);

            var query = new SqlLam<Post>();

            switch (sortColumn)
            {
                case "Title":
                    query = sortByAscending ? query.OrderBy(x => x.Title) : query.OrderByDescending(x => x.Title);
                    break;
                case "Published":
                    query = sortByAscending ? query.OrderBy(x => x.Published) : query.OrderByDescending(x => x.Published);
                    break;
                case "Modified":
                    query = sortByAscending ? query.OrderBy(x => x.Modified) : query.OrderByDescending(x => x.Modified);
                    break;
                default:
                    query = sortByAscending ? query.OrderBy(x => x.PostedOn) : query.OrderByDescending(x => x.PostedOn);

                    break;
            }
            var posts = CONN.SQLQuery<Post>(query.QueryStringPage(pageSize, pageNo), query.QueryParameters);
            return posts.ToList();

        }