Skip to content

Add support for GroupBy#2694

Merged
JKamsker merged 1 commit intolitedb-org:devfrom
JKamsker:feat/group-by
Oct 1, 2025
Merged

Add support for GroupBy#2694
JKamsker merged 1 commit intolitedb-org:devfrom
JKamsker:feat/group-by

Conversation

@JKamsker
Copy link
Copy Markdown
Collaborator

@JKamsker JKamsker commented Oct 1, 2025

This pull request introduces comprehensive support for LINQ-style GroupBy queries in LiteDB, enabling users to group query results using strongly-typed expressions and work with IGrouping<TKey, TElement> results. The changes span the query interface, query pipeline, expression visitor, and type mapping to provide a seamless and efficient grouping experience.

Key changes:

LINQ GroupBy Support and Interface Updates

  • Added a strongly-typed GroupBy<K>(Expression<Func<T, K>> keySelector) method to the ILiteQueryable<T> interface, allowing users to group results and receive IGrouping<K, T> objects. The Select methods were also updated to return ILiteQueryable instead of ILiteQueryableResult for consistency. [1] [2] [3] [4]

Grouping Implementation

  • Introduced the LiteGrouping<TKey, TElement> class as a concrete implementation of IGrouping<TKey, TElement>, enabling materialization of grouping results.
  • Added BsonMapper.RegisterGroupingType to handle serialization and deserialization of groupings, ensuring correct mapping between BSON and .NET groupings.

Query Pipeline and Execution Enhancements

  • Refactored the query pipeline to support grouping, including proper ordering before grouping, handling of Having, and in-memory ordering of grouped results. Added new internal structures (GroupSource, GroupedResult) and logic for efficient grouping and ordering. [1] [2] [3] [4]
  • Updated query optimization to allow ORDER BY after grouping and to avoid redundant ordering when the grouping expression matches the index.

Expression Visitor and Resolver Improvements

  • Enhanced the LINQ expression visitor to recognize and correctly resolve IGrouping<,> types, enabling support for LINQ methods such as Select, Where, and aggregation functions on groupings. [1] [2] [3]
  • Introduced GroupingResolver to map LINQ methods and properties on IGrouping to LiteDB expressions.

These changes collectively enable robust, efficient, and type-safe group-by queries in LiteDB, aligning its LINQ capabilities more closely with standard .NET query patterns.

@JKamsker JKamsker merged commit 3114fda into litedb-org:dev Oct 1, 2025
2 checks passed
@JKamsker JKamsker deleted the feat/group-by branch October 1, 2025 21:45
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.

1 participant