Skip to content

Expressions Projections

quyvu edited this page Oct 14, 2026 · 3 revisions

Projections

Select and transform specific properties using projection syntax with aliases and computed expressions.

Projection Types

Regular Projection (from collection)

Orders.{Id, Status, Total}
Orders.{Id, Customer.Name, OrderDate}

Root Projection (from root object, no source)

{Id, Name, Email}
{Id, Country.Name, Status}

Aliases

Rename output properties using the as keyword:

{Id, Country.Name as CountryName, Status as OrderStatus}
Orders.{Id, Total as Amount, Customer.Name as Buyer}

Functions in Projection

Orders.{Id, Total:round(2) as RoundedTotal, Name:upper}
{CreatedAt:format('yyyy-MM-dd') as Date, Name:trim as CleanName}

Computed Expressions

Computed expressions require parentheses () and an alias:

{(Nickname ?? Name) as DisplayName}
{(Status = 'Done' ? 'Yes' : 'No') as IsDone}
{(Total:multiply(1.1)) as TotalWithTax}
Orders.{Id, (Items:count > 5 ? 'Large' : 'Small') as Size}

Usage Example

// DTOs
public class UserSummaryDto
{
    public string Id { get; set; }
    public string DisplayName { get; set; }
    public string Location { get; set; }
    public string Status { get; set; }
    public int TotalOrders { get; set; }
}
 
public class OrderSummaryDto
{
    public string Id { get; set; }
    public decimal Amount { get; set; }
    public string Buyer { get; set; }
    public string Size { get; set; }
}
 
public class UserProfileResponse
{
    public string UserId { get; set; }
    public UserSummaryDto Summary { get; set; }
    public List<OrderSummaryDto> RecentOrders { get; set; }
}
 
public class UserProfileResponseProfile : ProfileOf<UserProfileResponse>
{
    protected override void Configure()
    {
        UseDistributedKey<UserDistributedKey>()
            .Of(x => x.UserId)
            .For(x => x.Summary, @"{
                Id,
                (Nickname ?? Name) as DisplayName,
                (Address?.City ?? 'Unknown') as Location,
                (IsActive = true ? 'Active' : 'Inactive') as Status,
                Orders:count as TotalOrders
            }")
            .For(x => x.RecentOrders, @"Orders:where(Status = 'Done'):orderBy(OrderDate desc):limit(10).{
                Id,
                Total:round(2) as Amount,
                Customer.Name as Buyer,
                (Items:count > 5 ? 'Large' : 'Small') as Size
            }");
    }
}

Complex Projection Example

# Complex projection combining multiple features
Orders:where(Status = 'Done'):orderBy(OrderDate desc):limit(10).{
    Id,
    OrderDate:format('yyyy-MM-dd') as Date,
    Total:round(2) as Amount,
    Customer.Name as Buyer,
    (Items:count > 5 ? 'Bulk' : 'Standard') as OrderType,
    (Total > 500 ? 'High Value' : 'Regular') as ValueTier
}
 
# Root projection with multiple computed expressions
{
    Id,
    (Nickname ?? FirstName ?? 'Anonymous') as DisplayName,
    (Address?.City ?? Address?.Country ?? 'Unknown') as Location,
    (IsActive ? 'Active' : 'Inactive') as Status,
    Orders:count as TotalOrders,
    Orders:where(Status = 'Done'):sum(Total) as TotalSpent
}

Important Rules

  • Simple properties can be used directly: {Id, Name}
  • Nested properties use dot notation: {Country.Name}
  • Use as for aliases: {Name as FullName}
  • Computed expressions need parentheses + alias: {(A ?? B) as Value}
  • Functions can be used with or without alias: {Name:upper} or {Name:upper as UpperName}

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

Clone this wiki locally