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
This commit is contained in:
Matt Furden 2018-05-27 18:33:10 -07:00
parent 26d9b09bf7
commit cd561e9f3d

View File

@ -142,7 +142,7 @@ namespace Sieve.Services
? converter.ConvertFrom(filterTerm.Value) ? converter.ConvertFrom(filterTerm.Value)
: Convert.ChangeType(filterTerm.Value, property.PropertyType); : Convert.ChangeType(filterTerm.Value, property.PropertyType);
Expression filterValue = GetClosureOverConstant(constantVal); Expression filterValue = GetClosureOverConstant(constantVal, property.PropertyType);
dynamic propertyValue = Expression.PropertyOrField(parameterExpression, property.Name); dynamic propertyValue = Expression.PropertyOrField(parameterExpression, property.Name);
@ -221,10 +221,11 @@ namespace Sieve.Services
// Workaround to ensure that the filter value gets passed as a parameter in generated SQL from EF Core // 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 // See https://github.com/aspnet/EntityFrameworkCore/issues/3361
private Expression GetClosureOverConstant<T>(T constant) // 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>(T constant, Type targetType)
{ {
Expression<Func<T>> closure = () => constant; return Expression.Constant(constant, targetType);
return closure.Body;
} }
private IQueryable<TEntity> ApplySorting<TEntity>( private IQueryable<TEntity> ApplySorting<TEntity>(