Merge pull request #243 from dotnet-architecture/groupby-specification-example

Adding a GroupBy specification example
This commit is contained in:
Eric Fleming
2019-05-01 19:42:07 -04:00
committed by GitHub
3 changed files with 14 additions and 3 deletions

View File

@@ -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; }

View File

@@ -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,12 @@ namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{ {
OrderByDescending = orderByDescendingExpression; OrderByDescending = orderByDescendingExpression;
} }
//Not used anywhere at the moment, but someone requested an example of setting this up.
protected virtual void ApplyGroupBy(Expression<Func<T, object>> groupByExpression)
{
GroupBy = groupByExpression;
}
} }
} }

View File

@@ -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)
{ {