Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2947,9 +2947,7 @@ public static IQueryable<TEntity> IgnoreQueryFilters<TEntity>(
Expression.Call(
instance: null,
method: new Func<IQueryable<TEntity>, IReadOnlyCollection<string>, IQueryable<TEntity>>(IgnoreQueryFilters).Method,
// converting the collection to an array if it isn't already one to ensure consistent caching. Fixes #37112.
// #37212 may be a possible future solution providing broader capabilities around parameterizing collections.
arguments: [source.Expression, Expression.Constant(filterKeys is string[] ? filterKeys : filterKeys.ToArray())]))
arguments: [source.Expression, Expression.Constant(filterKeys)]))
: source;

#endregion
Expand Down
24 changes: 22 additions & 2 deletions src/EFCore/Query/ExpressionEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public int GetHashCode(Expression obj)
hash.Add(structuralEquatable.GetHashCode(StructuralComparisons.StructuralEqualityComparer));
break;

case IEnumerable enumerable:
foreach (var item in enumerable)
{
hash.Add(item?.GetHashCode() ?? 0);
}
break;

default:
hash.Add(constantExpression.Value);
break;
Expand Down Expand Up @@ -368,8 +375,21 @@ private static bool CompareConstant(ConstantExpression a, ConstantExpression b)
{
var (v1, v2) = (a.Value, b.Value);

return Equals(v1, v2)
|| (v1 is IStructuralEquatable array1 && array1.Equals(v2, StructuralComparisons.StructuralEqualityComparer));
if (Equals(v1, v2))
{
return true;
}

return v1 switch
{
IStructuralEquatable structuralEquatable1
=> structuralEquatable1.Equals(v2, StructuralComparisons.StructuralEqualityComparer),

IEnumerable enumerable1 when v2 is IEnumerable enumerable2
=> enumerable1.Cast<object?>().SequenceEqual(enumerable2.Cast<object?>()),

_ => false
};
}

private bool CompareGoto(GotoExpression a, GotoExpression b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public virtual async Task Named_query_filters_caching()
builder.EnableSensitiveDataLogging();
builder.LogTo(cacheLog.Add, filter: (eventid, _) => eventid.Name == CoreEventId.QueryCompilationStarting.Name);
});

using var context = contextFactory.CreateContext();

_ = context.Entities
.IgnoreQueryFilters(["ActiveFilter", "NameFilter"])
.ToList();
Expand All @@ -54,8 +56,6 @@ public virtual async Task Named_query_filters_caching()
.IgnoreQueryFilters(["NameFilter", "ActiveFilter"])
.ToList();

// #37212 - ExpressionEqualityComparer doesn't support collections besides an array,
// therefore we can't implement caching for different order of ignored filters
Assert.Equal(2, cacheLog.Count);
}

Expand Down
Loading