Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable nullable type expression for sorting #2

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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