Skip to content

feat(mapper): add CollectionNegationAsNotAny for negated conditions o… - #350

Draft
danmusk wants to merge 1 commit into
alirezanet:masterfrom
Dokflyt:feat/collection-negation-as-not-any
Draft

danmusk wants to merge 1 commit into
alirezanet:masterfrom
Dokflyt:feat/collection-negation-as-not-any

Conversation

@danmusk

@danmusk danmusk commented Oct 23, 2026

Copy link
Copy Markdown

Description

Adds an opt-in configuration, CollectionNegationAsNotAny (global and per mapper, default false), 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. So tags!=red matches a record tagged red and blue, and tags= never matches a record without tags. Simple type collections already use !Contains for != (#204), but maps with Select/Where don't, and a map can't work around it (DefaultIfEmpty() isn't supported, and custom operators also run inside Any).

With the option enabled:

Filter Default (unchanged) CollectionNegationAsNotAny = true
tags!=red p.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(..) unchanged
  • Empty and null collections match the negated conditions (with null checks: p.Tags == null || !p.Tags.Any(..)).
  • Works with Select, SelectMany and Where in the map, including indexer maps like (p, key) => p.Props.Where(x => x.Key == key).Select(x => x.Value).
  • For simple type collections != 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 the Contains shortcut, which would otherwise test for an element with the default value.
  • A negated condition inside parentheses isn't merged into the same Any as another condition on that collection, since !Any(..) can't share it.
  • EF Core translates it to NOT EXISTS (...), the same SQL as the hand-written !Any(..).
  • LINQ only; the Elasticsearch builder doesn't support nested collections.

Implementation: LinqQueryBuilder builds the positive condition (!= → =, !* → =*, !^ → ^, !$ → $, empty = → empty !=) and negates the resulting Any. With the option off, the generated expressions are exactly as before; the two identical Select branches now share one helper.

Fixes #349

Related: #204, #91

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • I have made corresponding changes to the documentation
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes

Tests

  • test/Gridify.Tests/GridifyNestedCollectionNegationTests.cs (17): default behaviour pinned, !=, !*, !^, !$, empty value, non-negated conditions unchanged, indexer map with Where, 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 Release and dotnet test -c Release: Gridify.Tests 481/481, EF SqlProvider 21/21, EF PostgreSql 33/33, EF 11/11, Elasticsearch 218/218.

Docs

gridifyGlobalConfiguration.md and gridifyMapper.md (new CollectionNegationAsNotAny sections, and a note under "Filtering on Nested Collections").

…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>
@what-the-diff

what-the-diff Bot commented Oct 23, 2026

Copy link
Copy Markdown

PR Summary

  • Adds an optional way to handle negated filters on nested collections. When enabled, the query checks that no collection element matches the condition. The default behavior remains unchanged.
  • Makes the setting configurable globally or per mapper. Mapper settings inherit the global value unless explicitly overridden.
  • Clarifies how negated and empty-value filters behave through documentation and examples.
  • Adds tests for different collection types and database queries to verify the new behavior and confirm existing defaults are preserved.

This branch has not been deployed

No deployments
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.

Negated conditions and empty values on nested collections should mean "no element matches"

1 participant