-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e3b65e
commit ec06ee7
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/CSESoftware.Repository/Builder/ExpressionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
|
||
namespace CSESoftware.Repository.Builder | ||
{ | ||
internal static class ExpressionExtensions | ||
{ | ||
internal static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right) | ||
{ | ||
var parameter = Expression.Parameter(typeof(T), "entity"); | ||
|
||
var leftVisitor = new ReplaceExpressionVisitor(left.Parameters.First(), parameter); | ||
var leftBody = leftVisitor.Visit(left.Body); | ||
|
||
var rightVisitor = new ReplaceExpressionVisitor(right.Parameters.First(), parameter); | ||
var rightBody = rightVisitor.Visit(right.Body); | ||
|
||
var body = Expression.AndAlso(leftBody!, rightBody!); | ||
|
||
return Expression.Lambda<Func<T, bool>>(body, parameter); | ||
} | ||
|
||
private class ReplaceExpressionVisitor(Expression oldValue, Expression newValue) : ExpressionVisitor | ||
{ | ||
public override Expression Visit(Expression expression) | ||
{ | ||
return expression == oldValue ? newValue : base.Visit(expression); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters