From 940e057685c1f88fb024a3d4626d19cea14e22bd Mon Sep 17 00:00:00 2001 From: Yannick De Paepe Date: Tue, 27 Apr 2021 16:42:41 +0200 Subject: [PATCH] Fix merge conflicts for: Let you choose the Culture used for TypeConversion in the processor #79 --- Sieve/Models/SieveOptions.cs | 4 +++- Sieve/Services/SieveProcessor.cs | 9 ++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Sieve/Models/SieveOptions.cs b/Sieve/Models/SieveOptions.cs index f0059ee..a207958 100644 --- a/Sieve/Models/SieveOptions.cs +++ b/Sieve/Models/SieveOptions.cs @@ -9,5 +9,7 @@ public int MaxPageSize { get; set; } = 0; public bool ThrowExceptions { get; set; } = false; + + public string CultureNameOfTypeConversion { get; set; } = "en"; } -} \ No newline at end of file +} diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index ab363fc..b872edb 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -171,6 +172,8 @@ namespace Sieve.Services return result; } + var cultureInfoToUseForTypeConversion = new CultureInfo(_options.Value.CultureNameOfTypeConversion ?? "en"); + Expression outerExpression = null; var parameter = Expression.Parameter(typeof(TEntity), "e"); foreach (var filterTerm in model.GetFiltersParsed()) @@ -202,7 +205,7 @@ namespace Sieve.Services var isFilterTermValueNull = filterTermValue.ToLower() == nullFilterValue; var filterValue = isFilterTermValueNull ? Expression.Constant(null, property.PropertyType) - : ConvertStringValueToConstantExpression(filterTermValue, property, converter); + : ConvertStringValueToConstantExpression(filterTermValue, property, converter, cultureInfoToUseForTypeConversion); if (filterTerm.OperatorIsCaseInsensitive) { @@ -275,10 +278,10 @@ namespace Sieve.Services : Expression.AndAlso(nullCheckExpression, Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type))); } - private Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter) + private Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter, CultureInfo cultureInfo) { dynamic constantVal = converter.CanConvertFrom(typeof(string)) - ? converter.ConvertFrom(value) + ? converter.ConvertFrom(null, cultureInfo, value) : Convert.ChangeType(value, property.PropertyType); return GetClosureOverConstant(constantVal, property.PropertyType);