Skip to content

Commit

Permalink
Removed OrdinalIgnoreCase; Added initial methos to the ExpressionBuil…
Browse files Browse the repository at this point in the history
…der class; Adjusted expression extension methods.
  • Loading branch information
alexmurari committed Oct 30, 2020
1 parent 8dadf59 commit 6dfe757
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Product>Exprelsior</Product>
<Authors>Alexandre Murari Junior (alexmurari)</Authors>
Expand All @@ -12,7 +12,9 @@
<PackageProjectUrl>https://github.com/alexmurari/Exprelsior/</PackageProjectUrl>
<!-- Keep package release notes section without indentation -->
<PackageReleaseNotes>
- Fixed query parser not working with short name properties.
- Removed the OrdinalIgnoreCase string comparison from calls to contains methods on string collections (EF Core doesn't support string comparers).
- Added initial methods to start building expressions to the ExpressionBuilder class.
- Changed the behavior of the extension methods that compose expressions (And, Or) so it handles initial values correctly.
</PackageReleaseNotes>
</PropertyGroup>
</Project>
21 changes: 17 additions & 4 deletions Exprelsior.Shared/Extensions/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ public static class ExpressionExtensions
/// </returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
var leftExprBody = new RebindParameterVisitor(right.Parameters[0], left.Parameters[0]).Visit(right.Body);
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(left.Body, leftExprBody ?? throw new InvalidOperationException()), left.Parameters);
Expression<Func<T, bool>> all = t => true;

if (left == all)
return right;

if (right == all)
return left;

var rightExprBody = new RebindParameterVisitor(right.Parameters[0], left.Parameters[0]).Visit(right.Body);
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(left.Body, rightExprBody ?? throw new InvalidOperationException()), left.Parameters);
}

/// <summary>
Expand All @@ -50,8 +58,13 @@ public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> le
/// </returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
var leftExprBody = new RebindParameterVisitor(right.Parameters[0], left.Parameters[0]).Visit(right.Body);
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(left.Body, leftExprBody ?? throw new InvalidOperationException()), left.Parameters);
Expression<Func<T, bool>> all = t => true;

if (left == all || right == all)
return all;

var rightExprBody = new RebindParameterVisitor(right.Parameters[0], left.Parameters[0]).Visit(right.Body);
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(left.Body, rightExprBody ?? throw new InvalidOperationException()), left.Parameters);
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions Exprelsior/ExpressionBuilder/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@
/// </summary>
public static class ExpressionBuilder
{
/// <summary>
/// Creates a binary lambda expression whose delegate function always returns <c>true</c>.
/// Used as initial value to build predicates.
/// </summary>
/// <typeparam name="T">
/// The type that contains the property to be compared.
/// </typeparam>
/// <returns>
/// A <see cref="Expression{TDelegate}"/> instance whose delegate function always returns <c>true</c>.
/// </returns>
public static Expression<Func<T, bool>> True<T>() => t => true;

/// <summary>
/// Creates a binary lambda expression whose delegate function always returns <c>false</c>.
/// Used as initial value to build predicates.
/// </summary>
/// <typeparam name="T">
/// The type that contains the property to be compared.
/// </typeparam>
/// <returns>
/// A <see cref="Expression{TDelegate}"/> instance whose delegate function always returns <c>false</c>.
/// </returns>
public static Expression<Func<T, bool>> False<T>() => t => true;

/// <summary>
/// Creates a lambda expression that represents accessing a property of an object of type <typeparamref name="T"/>.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Exprelsior/ExpressionBuilder/ExpressionMethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static MethodCallExpression BuildGenericCollectionContainsMethodCall(Ex
/// <returns>The call to the method.</returns>
internal static MethodCallExpression BuildGenericStringCollectionContainsMethodCall(Expression property, Expression value)
{
return BuildEnumerableContainsMethodCall(property, value, StringComparer.OrdinalIgnoreCase);
return BuildEnumerableContainsMethodCall(property, value, null);
}

/// <summary>
Expand Down

0 comments on commit 6dfe757

Please sign in to comment.