Disable the use of the nullable type expression for sorting (#171)

* Added option to disable creation of sort expression for nullable types

* Changed property name.

* Updated readme

Co-authored-by: Teun Leijten <tleijten@inforit.nl>
This commit is contained in:
HBTeun 2022-03-30 10:42:59 +02:00 committed by GitHub
parent 7aaadccf18
commit c1fadddfe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 6 deletions

View File

@ -129,7 +129,8 @@ Then you can add the configuration:
"DefaultPageSize": "int number: optional number to fallback to when no page argument is given. Set <=0 to disable paging if no pageSize is specified (default).",
"MaxPageSize": "int number: maximum allowed page size. Set <=0 to make infinite (default)",
"ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false",
"IgnoreNullsOnNotEqual": "boolean: ignore null values when filtering using is not equal operator? Default to true"
"IgnoreNullsOnNotEqual": "boolean: ignore null values when filtering using is not equal operator? Defaults to true",
"DisableNullableTypeExpressionForSorting": "boolean: disable the creation of nullable type expression for sorting. Some databases do not handle it (yet). Defaults to false"
}
}
```

View File

@ -12,9 +12,10 @@ namespace Sieve.Extensions
string fullPropertyName,
PropertyInfo propertyInfo,
bool desc,
bool useThenBy)
bool useThenBy,
bool disableNullableTypeExpression = false)
{
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo);
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo, disableNullableTypeExpression);
var command = desc
? (useThenBy ? "ThenByDescending" : "OrderByDescending")
@ -33,7 +34,8 @@ namespace Sieve.Extensions
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>
(
string fullPropertyName,
PropertyInfo propertyInfo
PropertyInfo propertyInfo,
bool disableNullableTypeExpression
)
{
var parameter = Expression.Parameter(typeof(TEntity), "e");
@ -52,7 +54,7 @@ namespace Sieve.Extensions
propertyValue = Expression.MakeMemberAccess(propertyValue, propertyInfo);
}
if (propertyValue.Type.IsNullable())
if (propertyValue.Type.IsNullable() && !disableNullableTypeExpression)
{
nullCheck = GenerateOrderNullCheckExpression(propertyValue, nullCheck);
}

View File

@ -11,5 +11,7 @@
public bool ThrowExceptions { get; set; } = false;
public bool IgnoreNullsOnNotEqual { get; set; } = true;
public bool DisableNullableTypeExpressionForSorting { get; set; } = false;
}
}

View File

@ -365,7 +365,7 @@ namespace Sieve.Services
if (property != null)
{
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy);
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy, Options.Value.DisableNullableTypeExpressionForSorting);
}
else
{