Skip to content

CSHARP-5973: Support SkipWhile/TakeWhile index overloads in LINQ provider#1954

Open
adelinowona wants to merge 6 commits intomongodb:mainfrom
adelinowona:csharp5973
Open

CSHARP-5973: Support SkipWhile/TakeWhile index overloads in LINQ provider#1954
adelinowona wants to merge 6 commits intomongodb:mainfrom
adelinowona:csharp5973

Conversation

@adelinowona
Copy link
Copy Markdown
Contributor

No description provided.

@adelinowona adelinowona requested a review from sanych-sun April 15, 2026 20:30
@adelinowona adelinowona requested a review from a team as a code owner April 15, 2026 20:30
@adelinowona adelinowona added the feature Adds new user-facing functionality. label Apr 15, 2026
Copilot AI review requested due to automatic review settings April 15, 2026 20:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds LINQ3 provider support for the indexed predicate overloads of SkipWhile/TakeWhile by translating them to $reduce with arrayIndexAs, and introduces tests/feature gating to validate behavior and server compatibility.

Changes:

  • Extend method reflection metadata to recognize SkipWhile((x, i) => ...) / TakeWhile((x, i) => ...) for both Enumerable and Queryable.
  • Update the SkipWhile/TakeWhile aggregation-expression translator and serializer deduction logic to handle the index parameter and emit $reduce: { arrayIndexAs: ... }.
  • Add translator, serializer-finder, and integration tests, plus a new Feature.ReduceArrayIndexAs gate for integration coverage.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/SkipWhileOrTakeWhileMethodToAggregationExpressionTranslatorTests.cs Adds AST-level unit tests for both non-indexed and indexed SkipWhile/TakeWhile translations.
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/SerializerFinders/EnumerableTests.cs Verifies serializer-finder resolves serializers for the new indexed overloads.
tests/MongoDB.Driver.Tests/Linq/Integration/SkipWhileOrTakeWhileWithIndexTests.cs Adds end-to-end integration tests for indexed overloads (gated on server feature support).
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/SkipWhileOrTakeWhileMethodToAggreggationExpressoinTranslator.cs Implements index-parameter handling and passes arrayIndexAs through to $reduce.
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ExpressionToAggregationExpressionTranslator.cs Generalizes TranslateLambdaBody to accept multiple parameter symbols (needed for indexed lambdas).
src/MongoDB.Driver/Linq/Linq3Implementation/SerializerFinders/SerializerFinderVisitMethodCall.cs Ensures the index parameter has an Int32Serializer when deducing serializers for indexed overloads.
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/QueryableMethod.cs Adds Queryable.SkipWhile/TakeWhile MethodInfo for indexed predicate overloads.
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/EnumerableOrQueryableMethod.cs Adds method sets for indexed overloads and includes them in relevant groupings.
src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/EnumerableMethod.cs Adds Enumerable.SkipWhile/TakeWhile MethodInfo for indexed predicate overloads.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs Updates reduce visitor to preserve the new ArrayIndexAs child node.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstReduceExpression.cs Adds ArrayIndexAs support to $reduce AST node rendering and updating.
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs Extends AstExpression.Reduce factory to accept arrayIndexAs.
src/MongoDB.Driver/Core/Misc/Feature.cs Introduces Feature.ReduceArrayIndexAs used to gate integration tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

LGTM + minor comment.
Also need a 2nd review from someone with LINQ expertise.


var queryable = withNestedAsQueryable ?
collection.AsQueryable().Select(x => x.A.AsQueryable().TakeWhile((item, i) => i < 3).ToArray()) :
collection.AsQueryable().Select(x => x.A.TakeWhile((item, i) => i < 3).ToArray());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's have a test somewhere that uses both item and i in the expression itself, like you did in Enumerable_Select_with_index_should_work

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@adelinowona adelinowona requested a review from sanych-sun April 16, 2026 15:09
@mongodb mongodb deleted a comment from adelinowona Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds new user-facing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants