mirror of
				https://github.com/Biarity/Sieve.git
				synced 2025-11-04 02:39:00 +01:00 
			
		
		
		
	Ensure that dynamic LINQ expressions for filters match with those generated by Queryable.Where (#22)
This commit is contained in:
		@@ -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>(T constant)
 | 
			
		||||
        {
 | 
			
		||||
            Expression<Func<T>> closure = () => constant;
 | 
			
		||||
            return closure.Body;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private IQueryable<TEntity> ApplySorting<TEntity>(
 | 
			
		||||
            ISieveModel<IFilterTerm, ISortTerm> model, 
 | 
			
		||||
            IQueryable<TEntity> result,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user