Repository navigation
Conversation
…n nested collections Opt-in configuration (global and per mapper, default false). With it enabled, negated conditions (!=, !*, !^, !$) and the empty value (field=) on nested collections mean "no element matches": !Any(x => <positive condition>) instead of Any(x => <negated condition>). Empty and null collections match. With the option off, the generated expressions are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PR Summary
|
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds an opt-in configuration,
CollectionNegationAsNotAny(global and per mapper, defaultfalse), that makes negated conditions and the empty value on nested collections mean "no element matches".Today a condition on a mapped collection is applied per element inside
Any, negations included. Sotags!=redmatches a record taggedredandblue, andtags=never matches a record without tags. Simple type collections already use!Containsfor!=(#204), but maps withSelect/Wheredon't, and a map can't work around it (DefaultIfEmpty()isn't supported, and custom operators also run insideAny).With the option enabled:
CollectionNegationAsNotAny = truetags!=redp.Tags.Any(t => t != "red")!p.Tags.Any(t => t == "red")tags!*re(also!^,!$)p.Tags.Any(t => !t.Contains("re"))!p.Tags.Any(t => t.Contains("re"))tags=p.Tags.Any(t => string.IsNullOrEmpty(t))!p.Tags.Any(t => !string.IsNullOrEmpty(t))tags=red,tags!=,=*,^,>…Any(..)p.Tags == null || !p.Tags.Any(..)).Select,SelectManyandWherein the map, including indexer maps like(p, key) => p.Props.Where(x => x.Key == key).Select(x => x.Value).!=gives the same expression as today (List<string> or any primitive collection != which translates to Linq Not Contains should check for null as well #204). The empty value skips theContainsshortcut, which would otherwise test for an element with the default value.Anyas another condition on that collection, since!Any(..)can't share it.NOT EXISTS (...), the same SQL as the hand-written!Any(..).Implementation:
LinqQueryBuilderbuilds the positive condition (!=→=,!*→=*,!^→^,!$→$, empty=→ empty!=) and negates the resultingAny. With the option off, the generated expressions are exactly as before; the two identicalSelectbranches now share one helper.Fixes #349
Related: #204, #91
Type of change
Checklist
Tests
test/Gridify.Tests/GridifyNestedCollectionNegationTests.cs(17): default behaviour pinned,!=,!*,!^,!$, empty value, non-negated conditions unchanged, indexer map withWhere, parentheses, simple type collections,DisableCollectionNullChecks, and the global default.test/EntityFrameworkSqlProviderIntegrationTests/CollectionNegationAsNotAnyTests.cs(4): SQL is identical to the hand-written!Any(..), and the default is unchanged.dotnet build -c Releaseanddotnet test -c Release: Gridify.Tests 481/481, EF SqlProvider 21/21, EF PostgreSql 33/33, EF 11/11, Elasticsearch 218/218.Docs
gridifyGlobalConfiguration.mdandgridifyMapper.md(newCollectionNegationAsNotAnysections, and a note under "Filtering on Nested Collections").