How axelheer/nein-linq works
1. What this repo is
- NeinLinq is a .NET LINQ-expression rewriting library, not an app.
- Its job is to take a user-written query, transform the expression tree, then hand the rewritten query back to a LINQ provider such as plain LINQ-to-Objects, Entity Framework 6, or Entity Framework Core.
2. Solution shape
- The solution is split into three production packages:
- NeinLinq — core, provider-agnostic expression/query tooling.
- NeinLinq.EntityFramework — EF6-specific query integration.
- NeinLinq.EntityFrameworkCore — EF Core-specific query integration.
- Test projects mirror those three packages, plus the core library.
3. Primary architecture boundary: query wrapping vs query execution
- The central pattern is: a ToXxx(...) extension returns a specialized IQueryable/provider wrapper.
- That wrapper intercepts query execution, rewrites the expression tree, and then delegates to the original query provider.
- This keeps the public API small while allowing different transformations to be plugged in.
4. Core rewrite engine
- The src/NeinLinq project contains the provider-neutral machinery:
- RewriteQueryProvider, RewriteQueryable, RewriteQueryEnumerable, RewriteQueryEnumerator
- RewriteQueryBuilder, RewriteQueryRewriter, RewriteQueryCleaner
- These types form the generic query pipeline used by all flavors.
- The rewrite step is the key “engine room” where expression trees are traversed and replaced.
5. Lambda injection subsystem
- InjectLambdaAttribute, InjectLambdaMetadata, InjectLambdaSignature, and InjectableQueryBuilder implement the “replace a method call with a lambda expression body” feature.
- NullsafeQueryBuilder and NullsafeQueryRewriter provide a related transformation that inserts null checks into expressions.
- SubstitutionQueryBuilder and SubstitutionQueryRewriter support whole-type / method substitution, useful for swapping provider-specific helpers in tests or alternate runtimes.
6. Translation helpers for reusable predicates and selectors
- PredicateTranslator / PredicateTranslation and SelectorTranslator / SelectorTranslation are separate subsystems for expression reuse across related types.
- These are not query providers themselves; they produce transformed expressions that can then be composed into a query.
- Architecturally, they sit “above” the rewrite engine and feed it more portable predicates/selectors.
7. Dynamic query support
- DynamicQuery, DynamicQueryable, DynamicExpression, and DynamicCompare provide string-driven filtering and ordering.
- This is a smaller auxiliary path that builds expression trees from runtime field/operator input.
- It complements, but does not replace, the typed translation and rewrite features.
8. Caching and binding support
- CachedExpression and ObjectCache reduce repeated compilation/lookup overhead for injected lambdas and reusable expressions.
- ParameterBinder handles parameter replacement/binding inside expression trees.
- These are implementation support layers that make repeated rewriting practical and efficient.
9. EF6 integration boundary
- NeinLinq.EntityFramework adapts the core rewrite pipeline to EF6’s queryable/provider model.
- The EF6 project contains RewriteDbQueryProvider, RewriteDbQueryable, RewriteDbQueryBuilder, and async-aware enumerable/enumerator types.
- Its builder variants (InjectableDbQueryBuilder, NullsafeDbQueryBuilder, SubstitutionDbQueryBuilder) mirror the core features but are specialized for EF6 query objects.
10. EF Core integration boundary
- NeinLinq.EntityFrameworkCore adapts the same concepts to EF Core’s internal query compilation path.
- It includes RewriteQueryCompiler, RewriteDbContextOptionsExtension, and RewriteDbContextOptionsBuilderExtensions.
- The important architectural detail is that EF Core integration is enabled through context options, so the rewrite hook is installed into the DbContext pipeline rather than only wrapping IQueryable.
11. Data flow through the system
- A user writes a normal query.
- They opt into a feature with a wrapper call such as inject/nullsafe/substitution or a provider-specific ToEntityInjectable / ToDbInjectable.
- The query expression is captured, rewritten by a QueryBuilder/Rewriter, optionally cleaned, then executed by the original provider.
- For EF6/EF Core, the rewritten expression still ends up in the provider’s normal translation/execution path.
12. Testing layout and verification strategy
- Each subsystem has corresponding unit tests in test/NeinLinq.Tests.
- EF6 and EF Core each have both “fake” and “real” tests, indicating two verification layers:
- lightweight tests for rewrite behavior,
- integration tests against actual provider behavior.
- This is a strong sign that the architecture is centered on expression semantics and provider compatibility, not just pure library logic.
13. How to read this repo as a diagram
- Draw one core “expression rewrite engine” box in the middle.
- Feed it from three feature boxes: injection, null-safety, substitution.
- Show two adapter boxes on the side: EF6 and EF Core.
- Show the dynamic query and predicate/selector translation helpers as upstream expression builders that also feed the core engine.
- The final output is always an IQueryable or provider-compatible expression passed back to the underlying LINQ provider.
Main entry points: NeinLinq Core API · RewriteQueryBuilder.cs Query builders · RewriteQueryProvider.cs Rewrite engine · NeinLinq.EntityFrameworkCore EF Core package
Components 19
Core rewrite
Provider-neutral expression rewriting, translation, and dynamic query helpers.
- Core API · Public entry points and reusable expression/query helpers.
src/NeinLinq - Rewrite engine · Generic query wrapping, rewriting, and cleanup pipeline.
src/NeinLinq/RewriteQueryProvider.cs - Query builders · Feature-specific builders and rewriters for injectable, nullsafe, and substitution flows.
src/NeinLinq/RewriteQueryBuilder.cs - Lambda inject · Method-call replacement with lambda bodies via injection metadata.
src/NeinLinq/InjectLambdaAttribute.cs - Null-safe rewrite · Adds null checks during expression rewriting.
src/NeinLinq/NullsafeQueryBuilder.cs - Substitution rewrite · Whole-type and method substitution support.
src/NeinLinq/SubstitutionQueryBuilder.cs - Predicate/selector translation · Reusable expression translators for related types.
src/NeinLinq/PredicateTranslator.cs - Dynamic query · String-driven filtering and ordering expression builders.
src/NeinLinq/DynamicQuery.cs - Support layers · Caching and parameter binding used by repeated rewrites.
src/NeinLinq/CachedExpression.cs
EF6 adapter
Integration layer that wraps EF6 queryables and providers.
- EF6 package · EF6-specific query integration package.
src/NeinLinq.EntityFramework - EF6 provider · Queryable/provider wrapper that intercepts execution and rewrites expressions.
src/NeinLinq.EntityFramework/RewriteDbQueryProvider.cs - EF6 builders · EF6-specialized injectable, nullsafe, and substitution builders.
src/NeinLinq.EntityFramework/RewriteDbQueryBuilder.cs
EF Core adapter
Integration layer that hooks rewriting into EF Core query compilation and options.
- EF Core package · EF Core integration package.
src/NeinLinq.EntityFrameworkCore - EF Core hook · Installs rewrite behavior through DbContext options and query compiler integration.
src/NeinLinq.EntityFrameworkCore/RewriteQueryCompiler.cs - EF Core provider · Queryable/provider wrapper used alongside EF Core query execution.
src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryProvider.cs - EF Core builders · EF Core-specialized injectable, nullsafe, and substitution builders.
src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryBuilder.cs
Tests
Unit and integration coverage for core and provider-specific behavior.
- Core tests · Unit tests for rewrite, translation, dynamic query, and support behavior.
test/NeinLinq.Tests - EF6 tests · Fake and real tests for EF6 integration.
test/NeinLinq.EntityFramework.Tests - EF Core tests · Fake and real tests for EF Core integration.
test/NeinLinq.EntityFrameworkCore.Tests
Connections 26
- Core API → Rewrite engine: uses · Public query entry points build on the generic rewrite pipeline.
- Core API → Predicate/selector translation: exposes · Translation helpers are part of the reusable public surface.
- Core API → Dynamic query: exposes · Dynamic query helpers build expression trees from runtime input.
- Core API → Support layers: relies on · Caching and binding support expression reuse and performance.
- Rewrite engine → Query builders: delegates to · The core pipeline is parameterized by feature-specific builders.
- Query builders → Lambda inject: implements · Injectable query behavior replaces method calls with lambda bodies.
- Query builders → Null-safe rewrite: implements · Null-safe behavior inserts null checks into expressions.
- Query builders → Substitution rewrite: implements · Substitution behavior swaps methods and types in expressions.
- Predicate/selector translation → Rewrite engine: feeds · Translated predicates and selectors become input to rewriting.
- Dynamic query → Rewrite engine: feeds · Dynamic expressions are rewritten before provider execution.
- Support layers → Query builders: supports · Cached expressions and parameter binding assist rewriting.
- EF6 package → EF6 provider: wraps · The EF6 package centers on a queryable/provider interception layer.
- EF6 provider → Rewrite engine: adapts · EF6 execution is funneled into the provider-neutral rewrite engine.
- EF6 package → EF6 builders: exposes · EF6-specific builder variants mirror the core feature set.
- EF6 builders → Query builders: specializes · EF6 builders reuse the core rewrite patterns for DbQuery objects.
- EF Core package → EF Core hook: installs · EF Core integration is enabled through context options and compiler hooks.
- EF Core package → EF Core provider: wraps · The EF Core package also provides a provider-facing wrapper path.
- EF Core hook → Rewrite engine: adapts · The query compiler hook sends queries through the shared rewrite engine.
- EF Core provider → Rewrite engine: adapts · EF Core query wrappers delegate rewritten expressions to the underlying provider.
- EF Core package → EF Core builders: exposes · EF Core-specialized builders mirror injectable, nullsafe, and substitution flows.
- EF Core builders → Query builders: specializes · EF Core builders are the provider-specific variants of the core builders.
- Core tests → Core API: verifies · Core tests cover rewriting, translation, dynamic queries, and helpers.
- EF6 tests → EF6 package: verifies · EF6 tests exercise fake and real provider behavior.
- EF Core tests → EF Core package: verifies · EF Core tests exercise fake and real provider behavior.
- EF6 tests → Core API: covers · EF6 behavior depends on the shared core engine.
- EF Core tests → Core API: covers · EF Core behavior depends on the shared core engine.