From cd561e9f3d1351d109e48b814d777ac23875ea5f Mon Sep 17 00:00:00 2001 From: Matt Furden Date: Sun, 27 May 2018 18:33:10 -0700 Subject: [PATCH] Use Expression.Constant to allow Nullable comparison Using Expression.Constant for GetClosureOverConstant. Came to this solution from reading: http://bradwilson.typepad.com/blog/2008/07/creating-nullab.html --- Sieve/Services/SieveProcessor.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 792355a..c542a38 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -142,7 +142,7 @@ namespace Sieve.Services ? converter.ConvertFrom(filterTerm.Value) : Convert.ChangeType(filterTerm.Value, property.PropertyType); - Expression filterValue = GetClosureOverConstant(constantVal); + Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType); dynamic propertyValue = Expression.PropertyOrField(parameterExpression, property.Name); @@ -219,12 +219,13 @@ namespace Sieve.Services } } - //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) + // 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 + // Expression.Constant passed the target type to allow Nullable comparison + // See http://bradwilson.typepad.com/blog/2008/07/creating-nullab.html + private Expression GetClosureOverConstant(T constant, Type targetType) { - Expression> closure = () => constant; - return closure.Body; + return Expression.Constant(constant, targetType); } private IQueryable ApplySorting(