Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add in GroupBy #133

Open
egiconsulting opened this issue Mar 25, 2020 · 2 comments
Open

add in GroupBy #133

egiconsulting opened this issue Mar 25, 2020 · 2 comments

Comments

@egiconsulting
Copy link

It would be nice to also include GroupBy

@djechelon
Copy link
Contributor

Tell me if you like I create a pull request to the authors

        /// <summary>
        /// Gets the <see cref="IPagedList{TEntity}"/> based on a predicate, orderby delegate and page information. This method default no-tracking query.
        /// </summary>
        /// <param name="groupByExpression">A function to group results by key</param>
        /// <param name="predicate">A function to test each element for a condition.</param>
        /// <param name="orderBy">A function to order elements.</param>
        /// <param name="having">A function to test each element of the grouped result set for a condition that is evaluated after grouping.</param>
        /// <param name="pageIndex">The index of page.</param>
        /// <param name="pageSize">The size of the page.</param>
        /// <param name="disableTracking"><c>True</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
        /// <param name="ignoreQueryFilters">Ignore query filters</param>
        /// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns>
        /// <remarks>This method default no-tracking query.</remarks>
        public virtual IPagedList<IGrouping<TGroup, TEntity>> GetPagedList<TGroup>(Func<IQueryable<TEntity>, IQueryable<IGrouping<TGroup, TEntity>>> groupByExpression,
                                                Expression<Func<TEntity, bool>> predicate = null,
                                                Func<IQueryable<IGrouping<TGroup, TEntity>>, IOrderedQueryable<IGrouping<TGroup, TEntity>>> orderBy = null,
                                                Expression<Func<IGrouping<TGroup, TEntity>, bool>> having = null,
                                                int pageIndex = 0,
                                                int pageSize = 20,
                                                bool disableTracking = true,
                                                bool ignoreQueryFilters = false)
        {
            IQueryable<TEntity> query = _dbSet;

            if (disableTracking)
            {
                query = query.AsNoTracking();
            }

            if (predicate != null)
            {
                query = query.Where(predicate);
            }

            if (ignoreQueryFilters)
            {
                query = query.IgnoreQueryFilters();
            }

            var group = groupByExpression(query);

            if (having != null)
            {
                group = group.Where(having);
            }

            if (orderBy != null)
            {
                group = orderBy(group);
            }

            return group.ToPagedList(pageIndex, pageSize);
        }

@djechelon
Copy link
Contributor

I also took the time to add the optional "having" clause to this method. Obviously the result set is counted by groups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants