-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
IQueryable<>.FilterByItems()
from https://stackoverflow.com/quest…
…ions/67666649/lambda-linq-with-contains-criteria-for-multiple-keywords/67666993#67666993 to replace `IQueryable<>.WhereOrContainsValues()` except its last usage in `SaverWithRevision.AddRevisionsWithDuplicateIndex()` since it's passing a visited non-generic expression @ ExtensionMethods.cs @ c#/crawler
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,4 +73,44 @@ public static IQueryable<TEntity> WhereOrContainsValues<TEntity, TToCompare>( | |
LinqKit.PredicateBuilder.New<TEntity>(), | ||
(innerPredicate, expressionFactory) => | ||
innerPredicate.And(expressionFactory(valueToCompare)))))); | ||
|
||
/// <see>https://stackoverflow.com/questions/67666649/lambda-linq-with-contains-criteria-for-multiple-keywords/67666993#67666993</see> | ||
public static IQueryable<T> FilterByItems<T, TItem>( | ||
Check failure on line 78 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / build (crawler)
Check failure on line 78 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / build (crawler)
Check failure on line 78 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / build (crawler)
Check failure on line 78 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / build (crawler)
Check failure on line 78 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (windows-latest) / build (crawler)
Check failure on line 78 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (windows-latest) / build (crawler)
|
||
this IQueryable<T> query, | ||
IEnumerable<TItem> items, | ||
Expression<Func<T, TItem, bool>> filterPattern, | ||
bool isOr = true) | ||
{ | ||
var predicate = items.Aggregate<TItem, Expression?>(null, (current, item) => | ||
{ | ||
var itemExpr = Expression.Constant(item); | ||
var itemCondition = FilterByItemsExpressionReplacer | ||
.Replace(filterPattern.Body, filterPattern.Parameters[1], itemExpr); | ||
return current == null | ||
? itemCondition | ||
: Expression.MakeBinary(isOr ? ExpressionType.OrElse : ExpressionType.AndAlso, | ||
Check failure on line 92 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / build (crawler)
Check failure on line 92 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / build (crawler)
|
||
current, | ||
itemCondition); | ||
}) ?? Expression.Constant(false); | ||
var filterLambda = Expression.Lambda<Func<T, bool>>(predicate, filterPattern.Parameters[0]); | ||
|
||
return query.Where(filterLambda); | ||
} | ||
|
||
private class FilterByItemsExpressionReplacer(IDictionary<Expression, Expression> replaceMap) : ExpressionVisitor | ||
Check failure on line 101 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / build (crawler)
Check failure on line 101 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / build (crawler)
Check warning on line 101 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / ReSharper
Check failure on line 101 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (windows-latest) / build (crawler)
Check warning on line 101 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / ReSharper
Check warning on line 101 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (windows-latest) / ReSharper
|
||
{ | ||
private readonly IDictionary<Expression, Expression> _replaceMap | ||
= replaceMap ?? throw new ArgumentNullException(nameof(replaceMap)); | ||
|
||
[return: NotNullIfNotNull(nameof(exp))] | ||
public override Expression? Visit(Expression? exp) => | ||
Check failure on line 107 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / build (crawler)
Check failure on line 107 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / build (crawler)
|
||
exp != null && _replaceMap.TryGetValue(exp, out var replacement) | ||
? replacement | ||
: base.Visit(exp); | ||
|
||
public static Expression Replace(Expression expr, Expression toReplace, Expression toExpr) => | ||
Check failure on line 112 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (ubuntu-latest) / build (crawler)
Check failure on line 112 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (macos-latest) / build (crawler)
Check failure on line 112 in c#/crawler/src/ExtensionMethods.cs GitHub Actions / runs-on (windows-latest) / build (crawler)
|
||
new FilterByItemsExpressionReplacer(new Dictionary<Expression, Expression> {{toReplace, toExpr}}) | ||
.Visit(expr); | ||
} | ||
} |