Skip to content

Commit

Permalink
Adding a GroupBy specification example
Browse files Browse the repository at this point in the history
  • Loading branch information
efleming18 committed Apr 21, 2019
1 parent 1632f7f commit 468df47
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/ApplicationCore/Interfaces/ISpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface ISpecification<T>
List<string> IncludeStrings { get; }
Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; }
Expression<Func<T, object>> GroupBy { get; }

int Take { get; }
int Skip { get; }
Expand Down
7 changes: 7 additions & 0 deletions src/ApplicationCore/Specifications/BaseSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected BaseSpecification(Expression<Func<T, bool>> criteria)
public List<string> IncludeStrings { get; } = new List<string>();
public Expression<Func<T, object>> OrderBy { get; private set; }
public Expression<Func<T, object>> OrderByDescending { get; private set; }
public Expression<Func<T, object>> GroupBy { get; private set; }

public int Take { get; private set; }
public int Skip { get; private set; }
Expand Down Expand Up @@ -43,5 +44,11 @@ protected virtual void ApplyOrderByDescending(Expression<Func<T, object>> orderB
{
OrderByDescending = orderByDescendingExpression;
}

protected virtual void ApplyGroupBy(Expression<Func<T, object>> groupByExpression)
{
GroupBy = groupByExpression;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public CatalogFilterPaginatedSpecification(int skip, int take, int? brandId, int
(!typeId.HasValue || i.CatalogTypeId == typeId))
{
ApplyPaging(skip, take);
ApplyGroupBy(i => new { i.CatalogType });
}
}
}
8 changes: 5 additions & 3 deletions src/Infrastructure/Data/SpecificationEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.eShopWeb.Infrastructure.Data
{
Expand Down Expand Up @@ -38,6 +35,11 @@ public static IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T>
query = query.OrderByDescending(specification.OrderByDescending);
}

if (specification.GroupBy != null)
{
query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
}

// Apply paging if enabled
if (specification.isPagingEnabled)
{
Expand Down

0 comments on commit 468df47

Please sign in to comment.