Skip to content

Conversation

@bittola
Copy link
Contributor

@bittola bittola commented Nov 12, 2025

Fixing named query cache misses by adding support for collections inconstant expression comparer

fixes #37112

  • I've read the guidelines for contributing and seen the walkthrough
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

@bittola bittola requested a review from a team as a code owner November 12, 2025 17:27
hash.Add(structuralEquatable.GetHashCode(StructuralComparisons.StructuralEqualityComparer));
break;

case IEnumerable enumerable when constantExpression.Value is not string:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some limitations with this approach. At this point we don't have any information about the intent. In some cases we don't care about the order of elements in the collection, nor we care about the type of the collection. We just care that the elements are the same in no particular order.

With the implementation above we get the cache hit only when the type of the collection and order of the elements is the same.
For example:

var result = context.Entities
    .IgnoreQueryFilters(["ActiveFilter", "NameFilter"])
    .ToList();
_ = context.Entities
    .IgnoreQueryFilters(["ActiveFilter", "NameFilter"])
    .ToList();

However, the following won't work:

var result = context.Entities
    .IgnoreQueryFilters(["ActiveFilter", "NameFilter"])
    .ToList();
_ = context.Entities
    .IgnoreQueryFilters(["NameFilter", "ActiveFilter"])
    .ToList();

it will work with sorted collection for example

var result = context.Entities
    .IgnoreQueryFilters(new SortedSet<string> { "ActiveFilter", "NameFilter" })
    .ToList();
_ = context.Entities
    .IgnoreQueryFilters(new SortedSet<string> { "NameFilter", "ActiveFilter" })
    .ToList();

The question is how to pass that information to the comparer that we care or not about the order. We only have a type information. If we are OK with this limitation, then fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing we may need to consider is to limit this to more specific interface like ICollection or so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiled query is never cached when named query filters is used

1 participant