Data Model - EmployeePropsClick to view data model used in all examples
Tag with priority for categorization.
/// <summary>
/// Tag with priority for categorization.
/// </summary>
public class Tag
{
public string Name { get; set; } = string.Empty;
public int Priority { get; set; }
public string? Description { get; set; }
}
/// <summary>
/// Project metrics for analytics.
/// </summary>
[RedbScheme("ProjectMetrics")]
public class ProjectMetricsProps
{
public long ProjectId { get; set; }
public long? TasksCompleted { get; set; }
public long? TasksTotal { get; set; }
public long? BugsFixed { get; set; }
public double? Budget { get; set; }
public long? TeamSize { get; set; }
public Tag[]? Tags { get; set; }
public string[]? Technologies { get; set; }
}
/// <summary>
/// Address with city, street and building details.
/// </summary>
public class Address
{
public string City { get; set; } = string.Empty;
public string Street { get; set; } = string.Empty;
public BuildingInfo? Building { get; set; }
}
/// <summary>
/// Building information with floor and amenities.
/// </summary>
public class BuildingInfo
{
public int Floor { get; set; }
public string Name { get; set; } = string.Empty;
public string[]? Amenities { get; set; }
public int[]? AccessCodes { get; set; }
public string[]? ParkingSpots { get; set; }
public int[]? ElevatorFloors { get; set; }
}
/// <summary>
/// Contact detail key-value pair.
/// </summary>
public class ContactDetail
{
public string Label { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
}
/// <summary>
/// Contact information (email, phone, etc).
/// </summary>
public class Contact
{
public string Type { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
public bool IsVerified { get; set; }
public int[]? NotificationHours { get; set; }
public ContactDetail[]? Metadata { get; set; }
}
/// <summary>
/// Department info with tags, metrics and nested budget data.
/// </summary>
public class Department
{
public string Name { get; set; } = string.Empty;
public int HeadCount { get; set; }
public string[] Projects { get; set; } = [];
public Tag[] Leaders { get; set; } = [];
public Dictionary<string, int>? BudgetByYear { get; set; }
}
/// <summary>
/// Employee props - main model for examples.
/// Demonstrates all supported types:
/// - Simple types (int, string, DateTime, long, decimal)
/// - Arrays (string[], int[])
/// - Business classes (Address with nested BuildingInfo)
/// - Array of business classes (Contact[])
/// - RedbObject references (CurrentProject, PastProjects[])
/// - Dictionary types (various key and value types)
/// </summary>
[RedbScheme("Employee")]
public class EmployeeProps
{
// Basic info
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public int Age { get; set; }
public DateTime HireDate { get; set; }
public string Position { get; set; } = string.Empty;
public decimal Salary { get; set; }
public string Department { get; set; } = string.Empty;
public string? EmployeeCode { get; set; }
// Skills and certifications (arrays)
public string[]? Skills { get; set; }
public int[]? SkillLevels { get; set; }
public string[]? Certifications { get; set; }
public int[]? CertificationYears { get; set; }
// Addresses (business classes)
public Address? HomeAddress { get; set; }
public Address? WorkAddress { get; set; }
public Address? EmergencyAddress { get; set; }
// Contacts (array of business classes)
public Contact[]? Contacts { get; set; }
// Project references (RedbObject)
public RedbObject<ProjectMetricsProps>? CurrentProject { get; set; }
public RedbObject<ProjectMetricsProps>[]? PastProjects { get; set; }
// Phone directory: extension -> phone number
public Dictionary<string, string>? PhoneDirectory { get; set; }
// Office locations by city
public Dictionary<string, Address>? OfficeLocations { get; set; }
// Bonus history by year
public Dictionary<int, decimal>? BonusByYear { get; set; }
// Department details with complex nested data (Pro)
public Dictionary<string, Department>? DepartmentHistory { get; set; }
// Composite key: (year, quarter) -> performance score (Pro)
public Dictionary<(int Year, string Quarter), string>? PerformanceReviews { get; set; }
// Project metrics by code (Pro)
public Dictionary<string, RedbObject<ProjectMetricsProps>>? ProjectMetrics { get; set; }
}
/// <summary>
/// Department props for tree examples (corporate hierarchy).
/// Used with TreeRedbObject for organizational structure.
/// Similar to CategoryTestProps in ConsoleTest.
/// </summary>
[RedbScheme("Department")]
public class DepartmentProps
{
/// <summary>Department name (e.g. "IT Department Moscow").</summary>
public string Name { get; set; } = string.Empty;
/// <summary>Description (e.g. "Software development team").</summary>
public string Description { get; set; } = string.Empty;
/// <summary>Is department active?</summary>
public bool IsActive { get; set; } = true;
/// <summary>Budget in USD.</summary>
public decimal Budget { get; set; }
/// <summary>Department code (e.g. "IT-MSK-DEV").</summary>
public string Code { get; set; } = string.Empty;
}
/// <summary>
/// City props for list examples (linked objects).
/// Used as target object for RedbListItem.Object reference.
/// </summary>
[RedbScheme("City")]
public class CityProps
{
/// <summary>City name.</summary>
public string Name { get; set; } = string.Empty;
/// <summary>Population count.</summary>
public int Population { get; set; }
/// <summary>Region or district.</summary>
public string Region { get; set; } = string.Empty;
/// <summary>Is capital city?</summary>
public bool IsCapital { get; set; }
/// <summary>GPS coordinates [lat, lon].</summary>
public double[] Coordinates { get; set; } = [];
}
/// <summary>
/// Person props demonstrating ListItem fields.
/// Shows single ListItem and List of ListItems usage.
/// </summary>
[RedbScheme("Person")]
public class PersonProps
{
/// <summary>Person name.</summary>
public string Name { get; set; } = string.Empty;
/// <summary>Age in years.</summary>
public int Age { get; set; }
/// <summary>Email address.</summary>
public string Email { get; set; } = string.Empty;
/// <summary>Single ListItem field (e.g. status from dictionary).</summary>
public RedbListItem? Status { get; set; }
/// <summary>Array of ListItems (e.g. roles from dictionary).</summary>
public List<RedbListItem>? Roles { get; set; }
}
/// <summary>
/// Product with a unique business key (V4 [RedbUnique]): the SKU is unique per scheme,
/// enforced by the database over the hash of the canonical value. Used by E003.
/// </summary>
public class ProductProps
{
/// <summary>Unique business key. NULL never participates in uniqueness.</summary>
[RedbUnique]
public string? Sku { get; set; }
/// <summary>Display name (not unique).</summary>
public string Title { get; set; } = string.Empty;
/// <summary>Price in USD.</summary>
public decimal Price { get; set; }
}
Window - OrderByRedb
OrderByRedb in Window Functions - order by base IRedbObject field. SQL: ORDER BY o._date_create (NO JOIN for order key!)
var sw = Stopwatch.StartNew();
// Window with OrderByRedb (base field) + Frame
var ordered = await redb.Query<EmployeeProps>()
.Take(50)
.WithWindow(w => w
.OrderByRedb(x => x.DateCreate) // Base field - NO JOIN!
.Frame(Frame.Rows().UnboundedPreceding()))
.SelectAsync(x => new
{
x.Name,
Created = x.DateCreate,
Salary = x.Props.Salary,
RunningCount = Win.Count()
});
sw.Stop();
var last = ordered.LastOrDefault();
var sw = Stopwatch.StartNew();
// Project only 3 fields from 20+ available
var projected = await redb.Query<EmployeeProps>()
.Take(100)
.Select(x => new
{
x.id,
x.Props.FirstName,
x.Props.Salary,
HomeCity = x.Props.HomeAddress!.City,
HomeFloor = x.Props.HomeAddress!.Building!.Floor
})
.ToListAsync();
// Compare with full load
var swFull = Stopwatch.StartNew();
var full = await redb.Query<EmployeeProps>().Take(100).ToListAsync();
swFull.Stop();
sw.Stop();
var first = projected.FirstOrDefault();
SelectProjectionPerformanceIRedbQueryable.Select
PG Pro126ms(100)
postgres.ef954ms(100)
postgres.pro.repeat61ms(100)
postgres.pro.cache61ms(100)
postgres.ef.repeat1044ms(100)
MS Pro117ms(100)
mssql.pro.repeat85ms(100)
mssql.pro.cache77ms(100)
sqlite.pro45ms(100)
sqlite.pro.repeat33ms(100)
sqlite.pro.cache24ms(100)
Array Aggregation - All Elements
Array aggregation - aggregate all elements of an array field. SkillLevels[] contains numeric values (1-5) for aggregation. For one element by its index see E150A.
var sw = Stopwatch.StartNew();
// Aggregate ALL elements of SkillLevels[] array
var allElements = await redb.Query<EmployeeProps>()
.AggregateAsync(x => new
{
TotalSkillPoints = Agg.Sum(x.Props.SkillLevels.Select(s => s)),
AvgSkillLevel = Agg.Average(x.Props.SkillLevels.Select(s => s)),
Count = Agg.Count()
});
sw.Stop();
ArrayAggregationSumAgg.SumAgg.Average
PG Pro163ms(100307)
postgres.ef45ms(100306)
postgres.pro.repeat156ms(100308)
postgres.pro.cache155ms(100309)
postgres.ef.repeat41ms(100307)
MS Pro72ms(100207)
mssql.pro.repeat71ms(100208)
mssql.pro.cache339ms(100209)
sqlite.pro146ms(100101)
sqlite.pro.repeat186ms(100105)
sqlite.pro.cache172ms(100106)
Array Aggregation - By Index
Array aggregation - aggregate one element of an array field by its index. SkillLevels[0] is the level of the first skill of each employee. For all elements see E150.
var sw = Stopwatch.StartNew();
// Aggregate specific index: SkillLevels[0] (first skill)
var firstElement = await redb.Query<EmployeeProps>()
.AggregateAsync(x => new
{
SumFirstSkill = Agg.Sum(x.Props.SkillLevels[0]),
AvgFirstSkill = Agg.Average(x.Props.SkillLevels[0])
});
sw.Stop();
ArrayAggregationIndexAgg.SumAgg.Average
PG Pro154ms(1)
postgres.ef29ms(1)
postgres.pro.repeat152ms(1)
postgres.pro.cache156ms(1)
postgres.ef.repeat28ms(1)
MS Pro69ms(1)
mssql.pro.repeat79ms(1)
mssql.pro.cache320ms(1)
sqlite.pro118ms(1)
sqlite.pro.repeat171ms(1)
sqlite.pro.cache151ms(1)
Where - Arithmetic Multiply
Demonstrates arithmetic multiplication in Where clause. Calculates annual salary (monthly * 12) and filters employees earning over 1M per year. Pro feature: server-side arithmetic expressions.
var sw = Stopwatch.StartNew();
// Find employees with annual salary > 1,000,000
// Server-side: Salary * 12 > 1000000
var query = redb.Query<EmployeeProps>()
.Where(e => e.Salary * 12 > 1_000_000m)
.Take(100);
// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);
var highEarners = await query.ToListAsync();
var totalCount = await redb.Query<EmployeeProps>()
.Where(e => e.Salary * 12 > 1_000_000m)
.CountAsync();
sw.Stop();
var first = highEarners.FirstOrDefault();
var annualSalary = first?.Props.Salary * 12 ?? 0;
ArithmeticMultiplyExpressionIRedbQueryable.Where
PG Pro134ms(40124)
postgres.ef897ms(40123)
postgres.pro.repeat106ms(40125)
postgres.pro.cache113ms(40126)
postgres.ef.repeat1222ms(40124)
MS Pro123ms(40084)
mssql.pro.repeat135ms(40085)
mssql.pro.cache113ms(40086)
sqlite.pro54ms(40041)
sqlite.pro.repeat73ms(40042)
sqlite.pro.cache65ms(40043)
Where - Arithmetic Add Fields
Demonstrates arithmetic operations combining multiple fields in Where clause. Creates a "scoring" formula using Age and Salary to find senior valuable employees. Pro feature: server-side arithmetic with multiple Props fields.
var sw = Stopwatch.StartNew();
// Combined scoring: Age * 1000 + Salary > 120000
// Example: Age 40, Salary 85000 -> 40*1000 + 85000 = 125000 > 120000
var query = redb.Query<EmployeeProps>()
.Where(e => e.Age * 1000 + e.Salary > 120_000m)
.Take(100);
// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);
var results = await query.ToListAsync();
var totalCount = await redb.Query<EmployeeProps>()
.Where(e => e.Age * 1000 + e.Salary > 120_000m)
.CountAsync();
sw.Stop();
var first = results.FirstOrDefault();
var score = first != null ? first.Props.Age * 1000 + (int)first.Props.Salary : 0;
ArithmeticAddMultiFieldIRedbQueryable.Where
PG Pro158ms(65195)
postgres.ef1007ms(65195)
postgres.pro.repeat120ms(65195)
postgres.pro.cache126ms(65195)
postgres.ef.repeat1123ms(65195)
MS Pro172ms(65130)
mssql.pro.repeat217ms(65130)
mssql.pro.cache191ms(65130)
sqlite.pro167ms(65065)
sqlite.pro.repeat242ms(65065)
sqlite.pro.cache208ms(65065)
Where - String ToLower
Demonstrates case-insensitive string search using ToLower() in Where clause. Finds employees whose last name contains "s" regardless of case (Smith, Jones, etc.). Pro feature: server-side LOWER() function.
Demonstrates Trim() function in Where clause to filter out empty or whitespace-only values. Finds employees with meaningful first names (not empty after trimming). Pro feature: server-side TRIM() function.
Demonstrates chaining multiple string functions in Where clause. Finds employees in departments with long uppercase names. Pro feature: server-side chained UPPER() + LENGTH() functions.
Demonstrates Math.Abs() in Where clause for range filtering around a target value. Finds employees aged 30-40 (within 5 years of 35). Pro feature: server-side ABS() function.