From e4445afbe6f4d741258cd609918c79cf396ec24e Mon Sep 17 00:00:00 2001 From: Fati Iseni Date: Thu, 23 Jun 2022 15:30:48 +0200 Subject: [PATCH] Added WithSpecification overload for specifications with Select. (#273) --- .../Extensions/DbSetExtensions.cs | 28 +++++++++++++++++-- .../Extensions/DbSetExtensions.cs | 28 +++++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs index 7ae435a3..03497123 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs @@ -8,7 +8,11 @@ namespace Ardalis.Specification.EntityFramework6 { public static class DbSetExtensions { - public static async Task> ToListAsync(this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class + public static async Task> ToListAsync( + this DbSet source, + ISpecification specification, + CancellationToken cancellationToken = default) + where TSource : class { var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); @@ -17,7 +21,11 @@ public static async Task> ToListAsync(this DbSet : specification.PostProcessingAction(result).ToList(); } - public static async Task> ToEnumerableAsync(this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class + public static async Task> ToEnumerableAsync( + this DbSet source, + ISpecification specification, + CancellationToken cancellationToken = default) + where TSource : class { var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); @@ -26,7 +34,21 @@ public static async Task> ToEnumerableAsync(this D : specification.PostProcessingAction(result); } - public static IQueryable WithSpecification(this IQueryable source, ISpecification specification, ISpecificationEvaluator evaluator = null) where TSource : class + public static IQueryable WithSpecification( + this IQueryable source, + ISpecification specification, + ISpecificationEvaluator evaluator = null) + where TSource : class + { + evaluator = evaluator ?? SpecificationEvaluator.Default; + return evaluator.GetQuery(source, specification); + } + + public static IQueryable WithSpecification( + this IQueryable source, + ISpecification specification, + ISpecificationEvaluator evaluator = null) + where TSource : class { evaluator = evaluator ?? SpecificationEvaluator.Default; return evaluator.GetQuery(source, specification); diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs index c27133f4..ed13f61a 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs @@ -8,7 +8,11 @@ namespace Ardalis.Specification.EntityFrameworkCore { public static class DbSetExtensions { - public static async Task> ToListAsync(this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class + public static async Task> ToListAsync( + this DbSet source, + ISpecification specification, + CancellationToken cancellationToken = default) + where TSource : class { var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); @@ -17,7 +21,11 @@ public static async Task> ToListAsync(this DbSet : specification.PostProcessingAction(result).ToList(); } - public static async Task> ToEnumerableAsync(this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class + public static async Task> ToEnumerableAsync( + this DbSet source, + ISpecification specification, + CancellationToken cancellationToken = default) + where TSource : class { var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); @@ -26,7 +34,21 @@ public static async Task> ToEnumerableAsync(this D : specification.PostProcessingAction(result); } - public static IQueryable WithSpecification(this IQueryable source, ISpecification specification, ISpecificationEvaluator? evaluator = null) where TSource : class + public static IQueryable WithSpecification( + this IQueryable source, + ISpecification specification, + ISpecificationEvaluator? evaluator = null) + where TSource : class + { + evaluator = evaluator ?? SpecificationEvaluator.Default; + return evaluator.GetQuery(source, specification); + } + + public static IQueryable WithSpecification( + this IQueryable source, + ISpecification specification, + ISpecificationEvaluator? evaluator = null) + where TSource : class { evaluator = evaluator ?? SpecificationEvaluator.Default; return evaluator.GetQuery(source, specification);