From fd4367cf373325a255f38eb9c5288133f6543432 Mon Sep 17 00:00:00 2001 From: Daniel Macko Date: Thu, 24 May 2018 14:16:10 +1000 Subject: [PATCH] Ensure that dynamic LINQ expressions for filters match with those generated by Queryable.Where (#22) --- Sieve/Services/SieveProcessor.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 7930b2d..5402602 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -127,10 +127,12 @@ namespace Sieve.Services var converter = TypeDescriptor.GetConverter(property.PropertyType); var parameter = Expression.Parameter(typeof(TEntity), "e"); - dynamic filterValue = Expression.Constant( - converter.CanConvertFrom(typeof(string)) ? - converter.ConvertFrom(filterTerm.Value) : - Convert.ChangeType(filterTerm.Value, property.PropertyType)); + + dynamic constantVal = converter.CanConvertFrom(typeof(string)) + ? converter.ConvertFrom(filterTerm.Value) + : Convert.ChangeType(filterTerm.Value, property.PropertyType); + + Expression filterValue = GetClosureOverConstant(constantVal); dynamic propertyValue = Expression.PropertyOrField(parameter, property.Name); @@ -201,6 +203,14 @@ namespace Sieve.Services return result; } + //Workaround to ensure that the filter value gets passed as a parameter in generated SQL from EF Core + //See https://github.com/aspnet/EntityFrameworkCore/issues/3361 + private Expression GetClosureOverConstant(T constant) + { + Expression> closure = () => constant; + return closure.Body; + } + private IQueryable ApplySorting( ISieveModel model, IQueryable result,