Created
October 11, 2019 09:47
-
-
Save Rudyzio/ae86cd9d4009a967dd303a26d797c68f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SpecificationEvaluator<TEntity> where TEntity : BaseEntity | |
{ | |
public static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery, ISpecification<TEntity> specification) | |
{ | |
var query = inputQuery; | |
// modify the IQueryable using the specification's criteria expression | |
if (specification.Criteria != null) | |
{ | |
query = query.Where(specification.Criteria); | |
} | |
// Includes all expression-based includes | |
query = specification.Includes.Aggregate(query, | |
(current, include) => current.Include(include)); | |
// Include any string-based include statements | |
query = specification.IncludeStrings.Aggregate(query, | |
(current, include) => current.Include(include)); | |
// Apply ordering if expressions are set | |
if (specification.OrderBy != null) | |
{ | |
query = query.OrderBy(specification.OrderBy); | |
} | |
else if (specification.OrderByDescending != null) | |
{ | |
query = query.OrderByDescending(specification.OrderByDescending); | |
} | |
if (specification.GroupBy != null) | |
{ | |
query = query.GroupBy(specification.GroupBy).SelectMany(x => x); | |
} | |
// Apply paging if enabled | |
if (specification.IsPagingEnabled) | |
{ | |
query = query.Skip(specification.Skip) | |
.Take(specification.Take); | |
} | |
return query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment