.Group<T>(groupBy)
Groups the query by the specified fields and applies aggregations. Returns a dynamic IQueryable whose elements expose the group-by fields and aggregate aliases as properties.
Signature
public static IQueryable Group<T>(this IQueryable<T> query, GroupBy groupBy)
where T : class| Parameter | Type | Description |
|---|---|---|
groupBy | GroupBy | Grouping and aggregation config |
Validations
- Must have at least one field —
GroupByMustHaveFields. - Fields must be unique (case-insensitive) —
GroupByFieldsMustBeUnique. - Fields cannot be complex/navigation types —
GroupByFieldCannotBeComplexType. - Fields cannot be collection types —
GroupByFieldCannotBeCollectionType. - Aggregation alias must not be empty and must not contain dots —
InvalidAlias. - Aggregation aliases must be unique —
AggregationAliasesMustBeUnique. - Aggregation alias cannot match a GroupBy field —
AggregationAliasCannotBeGroupByField({alias}). - Aggregation field must be a simple type —
AggregationFieldMustBeSimpleType. - Aggregation field cannot be a collection —
AggregationFieldCannotBeCollectionType. Sumation/Averageonly work on numeric fields —UnsupportedAggregatorForType.Minimum/Maximumdo not work onBoolean—UnsupportedAggregatorForType.
Warning
Dotted
GroupBy fields (e.g., Category.Name) produce flattened alias keys in the dynamic result objects (e.g., CategoryName). Order fields in Summary.Orders should use the dotted form; the library handles alias mapping internally.Returns
IQueryable — a dynamic query with grouped results. Each element exposes the group-by fields (flattened to alias keys) and the aggregation aliases.
Example
var groupBy = new GroupBy
{
Fields = new List<string> { "Category" },
AggregateBy = new List<AggregateBy>
{
new AggregateBy { Field = null, Alias = "TotalCount", Aggregator = Aggregator.Count },
new AggregateBy { Field = "Price", Alias = "AvgPrice", Aggregator = Aggregator.Average },
new AggregateBy { Field = "Price", Alias = "MaxPrice", Aggregator = Aggregator.Maximum }
}
};
var grouped = dbContext.Products.Group(groupBy);{
"fields": ["Category"],
"aggregateBy": [
{ "field": null, "alias": "TotalCount", "aggregator": "Count" },
{ "field": "Price", "alias": "AvgPrice", "aggregator": "Average" },
{ "field": "Price", "alias": "MaxPrice", "aggregator": "Maximum" }
]
}See also
GroupBy/AggregateByshapes.Aggregatorvalues.- GroupBy validation.
.Summary<T>for the fullwhere → group → having → order → pagepipeline.