Class Aggregate
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents an aggregate function operation (COUNT, SUM, AVG, MIN, MAX) that can be used in expressions.
public class Aggregate : IElement
- Inheritance
-
Aggregate
- Implements
Examples
Using aggregate in an expression (advanced):
// Create an aggregate: SUM(price * quantity)
Expression priceQtyExpression = new Expression();
priceQtyExpression.Add(ordersTable["price"]);
priceQtyExpression.Add(new ArithmeticOperator(ArithmeticType.Multiply));
priceQtyExpression.Add(ordersTable["quantity"]);
Aggregate sumAggregate = new Aggregate(AggregateType.Sum, priceQtyExpression.Elements);
// Note: For this scenario, use Function instead:
Function sumFunction = new Function(FunctionType.Sum);
sumFunction.Add(ordersTable["price"]);
sumFunction.Add(new ArithmeticOperator(ArithmeticType.Multiply));
sumFunction.Add(ordersTable["quantity"]);
Preferred approach using Function:
// COUNT aggregate
Function countFunction = new Function(FunctionType.Count, ordersTable["id"]);
query.AddSelectItem(countFunction);
// SUM aggregate
Function sumFunction = new Function(FunctionType.Sum, ordersTable["total"]);
query.AddSelectItem(sumFunction);
// AVG aggregate
Function avgFunction = new Function(FunctionType.Avg, ordersTable["price"]);
query.AddSelectItem(avgFunction);
// MIN aggregate
Function minFunction = new Function(FunctionType.Min, ordersTable["price"]);
query.AddSelectItem(minFunction);
// MAX aggregate
Function maxFunction = new Function(FunctionType.Max, ordersTable["price"]);
query.AddSelectItem(maxFunction);
Remarks
Aggregate performs aggregate calculations on sets of rows, typically used with GROUP BY clauses. While Function is the more common way to use aggregate functions, Aggregate provides a lower-level building block for complex scenarios where aggregates need to be composed with other elements in expressions.
The supported aggregate types are:
- CountCounts the number of rows
- SumCalculates the sum of values
- AvgCalculates the average of values
- MinFinds the minimum value
- MaxFinds the maximum value
For most use cases, prefer using Function with FunctionType aggregate types instead of using Aggregate directly. Aggregate is primarily used internally and in advanced scenarios.
Constructors
- Aggregate(AggregateType)
Creates a new aggregate without any elements.
- Aggregate(AggregateType, IEnumerable<IElement>)
Creates a new aggregate with multiple elements.
- Aggregate(AggregateType, IElement)
Creates a new aggregate with a single element.
- Aggregate(AggregateType, params IElement[])
Creates a new aggregate with multiple elements.
Properties
- AggregateType
Gets or sets the type of aggregate operation (Count, Sum, Avg, Min, Max).
- Elements
Gets or sets the elements (columns, expressions, etc.) that the aggregate operates on.