Skip to content

Commit

Permalink
Disable the use of the nullable type expression for sorting (#171)
Browse files Browse the repository at this point in the history
* Added option to disable creation of sort expression for nullable types

* Changed property name.

* Updated readme

Co-authored-by: Teun Leijten <[email protected]>
  • Loading branch information
HBTeun and Teun Leijten authored Mar 30, 2022
1 parent 7aaadcc commit c1faddd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
Expand Down
10 changes: 6 additions & 4 deletions Sieve/Extensions/OrderByDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public static IQueryable<TEntity> OrderByDynamic<TEntity>(
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")
Expand All @@ -33,7 +34,8 @@ public static IQueryable<TEntity> OrderByDynamic<TEntity>(
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>
(
string fullPropertyName,
PropertyInfo propertyInfo
PropertyInfo propertyInfo,
bool disableNullableTypeExpression
)
{
var parameter = Expression.Parameter(typeof(TEntity), "e");
Expand All @@ -52,7 +54,7 @@ PropertyInfo propertyInfo
propertyValue = Expression.MakeMemberAccess(propertyValue, propertyInfo);
}

if (propertyValue.Type.IsNullable())
if (propertyValue.Type.IsNullable() && !disableNullableTypeExpression)
{
nullCheck = GenerateOrderNullCheckExpression(propertyValue, nullCheck);
}
Expand Down
2 changes: 2 additions & 0 deletions Sieve/Models/SieveOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class SieveOptions
public bool ThrowExceptions { get; set; } = false;

public bool IgnoreNullsOnNotEqual { get; set; } = true;

public bool DisableNullableTypeExpressionForSorting { get; set; } = false;
}
}
2 changes: 1 addition & 1 deletion Sieve/Services/SieveProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ protected virtual IQueryable<TEntity> ApplySorting<TEntity>(TSieveModel model, I

if (property != null)
{
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy);
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy, Options.Value.DisableNullableTypeExpressionForSorting);
}
else
{
Expand Down

0 comments on commit c1faddd

Please sign in to comment.