Browse Source

Adding a GroupBy specification example

main
Eric Fleming 7 years ago
parent
commit
468df47c22
  1. 1
      src/ApplicationCore/Interfaces/ISpecification.cs
  2. 7
      src/ApplicationCore/Specifications/BaseSpecification.cs
  3. 1
      src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs
  4. 8
      src/Infrastructure/Data/SpecificationEvaluator.cs

1
src/ApplicationCore/Interfaces/ISpecification.cs

@ -11,6 +11,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
List<string> IncludeStrings { get; } List<string> IncludeStrings { get; }
Expression<Func<T, object>> OrderBy { get; } Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; } Expression<Func<T, object>> OrderByDescending { get; }
Expression<Func<T, object>> GroupBy { get; }
int Take { get; } int Take { get; }
int Skip { get; } int Skip { get; }

7
src/ApplicationCore/Specifications/BaseSpecification.cs

@ -16,6 +16,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
public List<string> IncludeStrings { get; } = new List<string>(); public List<string> IncludeStrings { get; } = new List<string>();
public Expression<Func<T, object>> OrderBy { get; private set; } public Expression<Func<T, object>> OrderBy { get; private set; }
public Expression<Func<T, object>> OrderByDescending { 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 Take { get; private set; }
public int Skip { get; private set; } public int Skip { get; private set; }
@ -43,5 +44,11 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{ {
OrderByDescending = orderByDescendingExpression; OrderByDescending = orderByDescendingExpression;
} }
protected virtual void ApplyGroupBy(Expression<Func<T, object>> groupByExpression)
{
GroupBy = groupByExpression;
}
} }
} }

1
src/ApplicationCore/Specifications/CatalogFilterPaginatedSpecification.cs

@ -9,6 +9,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
(!typeId.HasValue || i.CatalogTypeId == typeId)) (!typeId.HasValue || i.CatalogTypeId == typeId))
{ {
ApplyPaging(skip, take); ApplyPaging(skip, take);
ApplyGroupBy(i => new { i.CatalogType });
} }
} }
} }

8
src/Infrastructure/Data/SpecificationEvaluator.cs

@ -1,10 +1,7 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
namespace Microsoft.eShopWeb.Infrastructure.Data namespace Microsoft.eShopWeb.Infrastructure.Data
{ {
@ -38,6 +35,11 @@ namespace Microsoft.eShopWeb.Infrastructure.Data
query = query.OrderByDescending(specification.OrderByDescending); query = query.OrderByDescending(specification.OrderByDescending);
} }
if (specification.GroupBy != null)
{
query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
}
// Apply paging if enabled // Apply paging if enabled
if (specification.isPagingEnabled) if (specification.isPagingEnabled)
{ {

Loading…
Cancel
Save