diff --git a/src/ApplicationCore/Interfaces/ISpecification.cs b/src/ApplicationCore/Interfaces/ISpecification.cs index a61420f..f67414a 100644 --- a/src/ApplicationCore/Interfaces/ISpecification.cs +++ b/src/ApplicationCore/Interfaces/ISpecification.cs @@ -11,6 +11,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces List IncludeStrings { get; } Expression> OrderBy { get; } Expression> OrderByDescending { get; } + Expression> GroupBy { get; } int Take { get; } int Skip { get; } diff --git a/src/ApplicationCore/Specifications/BaseSpecification.cs b/src/ApplicationCore/Specifications/BaseSpecification.cs index 1017f3d..f87b8d5 100644 --- a/src/ApplicationCore/Specifications/BaseSpecification.cs +++ b/src/ApplicationCore/Specifications/BaseSpecification.cs @@ -16,6 +16,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications public List IncludeStrings { get; } = new List(); public Expression> OrderBy { get; private set; } public Expression> OrderByDescending { get; private set; } + public Expression> GroupBy { get; private set; } public int Take { get; private set; } public int Skip { get; private set; } @@ -43,5 +44,12 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications { OrderByDescending = orderByDescendingExpression; } + + //Not used anywhere at the moment, but someone requested an example of setting this up. + protected virtual void ApplyGroupBy(Expression> groupByExpression) + { + GroupBy = groupByExpression; + } + } } diff --git a/src/Infrastructure/Data/SpecificationEvaluator.cs b/src/Infrastructure/Data/SpecificationEvaluator.cs index 88910d2..f029066 100644 --- a/src/Infrastructure/Data/SpecificationEvaluator.cs +++ b/src/Infrastructure/Data/SpecificationEvaluator.cs @@ -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 { @@ -38,6 +35,11 @@ namespace Microsoft.eShopWeb.Infrastructure.Data query = query.OrderByDescending(specification.OrderByDescending); } + if (specification.GroupBy != null) + { + query = query.GroupBy(specification.GroupBy).SelectMany(x => x); + } + // Apply paging if enabled if (specification.isPagingEnabled) {