Skip to content

Expressions Aggregations

quyvu edited this page Oct 14, 2026 · 3 revisions

Aggregations

Aggregations operate on collections and return scalar values like count, sum, average, min, and max.

Available Aggregations

Aggregation Syntax Example Description
Count :count Orders:count Count items
Sum :sum(property) Orders:sum(Total) Sum of values
Average :avg(property) Orders:avg(Total) Average value
Minimum :min(property) Orders:min(Total) Minimum value
Maximum :max(property) Orders:max(Total) Maximum value

Basic Usage

public class UserStatsResponse
{
    public string UserId { get; set; }
    public int TotalOrders { get; set; }
    public decimal TotalSpent { get; set; }
    public decimal AverageOrderValue { get; set; }
    public decimal SmallestOrder { get; set; }
    public decimal LargestOrder { get; set; }
}
 
public class UserStatsResponseProfile : ProfileOf<UserStatsResponse>
{
    protected override void Configure()
    {
        UseDistributedKey<UserDistributedKey>()
            .Of(x => x.UserId)
            .For(x => x.TotalOrders, "Orders:count")
            .For(x => x.TotalSpent, "Orders:sum(Total)")
            .For(x => x.AverageOrderValue, "Orders:avg(Total)")
            .For(x => x.SmallestOrder, "Orders:min(Total)")
            .For(x => x.LargestOrder, "Orders:max(Total)");
    }
}

Aggregation with Filter

Combine filters with aggregations to get specific statistics:

# Count completed orders
Orders:where(Status = 'Done'):count
 
# Sum of completed orders
Orders:where(Status = 'Done'):sum(Total)
 
# Average for 2024
Orders:where(Year = 2024):avg(Total)
 
# Max order in Q1
Orders:where(Quarter = 1):max(Total)
 
# Count high-value orders
Orders:where(Total > 1000):count

Usage Example with Filters

public class UserAnalyticsResponse
{
    public string UserId { get; set; }
    public int CompletedOrdersCount { get; set; }
    public decimal CompletedOrdersTotal { get; set; }
    public decimal AvgOrderThisYear { get; set; }
    public decimal LargestEverOrder { get; set; }
    public DateTime FirstOrderDate { get; set; }
    public DateTime LastOrderDate { get; set; }
    public decimal PendingOrdersTotal { get; set; }
}
 
public class UserAnalyticsResponseProfile : ProfileOf<UserAnalyticsResponse>
{
    protected override void Configure()
    {
        UseDistributedKey<UserDistributedKey>()
            .Of(x => x.UserId)
            .For(x => x.CompletedOrdersCount, "Orders:where(Status = 'Done'):count")
            .For(x => x.CompletedOrdersTotal, "Orders:where(Status = 'Done'):sum(Total)")
            .For(x => x.AvgOrderThisYear, "Orders:where(CreatedAt:year = 2024):avg(Total)")
            .For(x => x.LargestEverOrder, "Orders:max(Total)")
            .For(x => x.FirstOrderDate, "Orders:min(CreatedAt)")
            .For(x => x.LastOrderDate, "Orders:max(CreatedAt)")
            .For(x => x.PendingOrdersTotal, "Orders:where(Status = 'Pending'):sum(Total)");
    }
}

Boolean Functions

Boolean functions return true/false and operate on collections.

Any

Check if at least one item matches the condition:

Orders:any                              # true if has any orders
Orders:any(Status = 'Done')             # true if at least 1 order is Done
Orders:any(Total > 1000)                # true if any order > 1000

All

Check if all items match the condition:

Documents:all(IsApproved = true)        # true if all are approved
Items:all(Quantity > 0)                 # true if all quantities > 0
Orders:all(Status != 'Cancelled')       # true if no orders cancelled

Boolean Functions in Conditions

Use boolean functions with ternary operator:

# In expressions
Orders:any(Status = 'Overdue') ? 'Has Overdue' : 'All Current'
 
# In projections
{
  (Orders:any(Status = 'Overdue') ? 'Warning' : 'OK') as Alert,
  (Items:all(InStock = true) ? 'Ready' : 'Pending') as StockStatus
}

Boolean Functions Example

public class OrderStatusResponse
{
    public string UserId { get; set; }
    public bool HasOrders { get; set; }
    public bool HasOverdueOrders { get; set; }
    public bool AllOrdersCompleted { get; set; }
    public bool HasHighValueOrders { get; set; }
    public string AlertStatus { get; set; }
}
 
public class OrderStatusResponseProfile : ProfileOf<OrderStatusResponse>
{
    protected override void Configure()
    {
        UseDistributedKey<UserDistributedKey>()
            .Of(x => x.UserId)
            .For(x => x.HasOrders, "Orders:any")
            .For(x => x.HasOverdueOrders, "Orders:any(Status = 'Overdue')")
            .For(x => x.AllOrdersCompleted, "Orders:all(Status = 'Done')")
            .For(x => x.HasHighValueOrders, "Orders:any(Total > 1000)")
            .For(x => x.AlertStatus, "Orders:any(Status = 'Overdue') ? 'Warning' : 'OK'");
    }
}

Source: https://fxmapper.net/docs/expressions/aggregations/ — the website is always the most up to date version.

Clone this wiki locally