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; }
}
Scalar + Array Combined
Demonstrates combining scalar field filter with array Contains filter. Finds senior developers (Age > 30) who have specific skills. Pro feature: mixed scalar and array expressions in single query.
Demonstrates String.Length property in Where clause. Server-side: LENGTH() in PostgreSQL, LEN() in MSSQL. Pro feature: string property access in expressions.
Demonstrates GroupBy aggregation on TreeQuery. Groups tree nodes and calculates aggregates (SUM, COUNT) within tree context. Pro feature: TreeQuery + GroupBy for hierarchical data analysis.
Demonstrates Window Functions on TreeQuery. Calculates ROW_NUMBER and running totals within tree hierarchy. Pro feature: TreeQuery + Window for hierarchical rankings and cumulative values.
Demonstrates GroupBy + Window Functions on TreeQuery. First groups tree nodes, then applies window functions to rank the groups. Pro feature: TreeQuery + GroupBy + Window for advanced hierarchical analytics.
Demonstrates DistinctBy on TreeQuery to get unique tree nodes by a Props field. Useful for removing duplicates based on specific property values. Pro feature: DistinctBy with tree context.
var sw = Stopwatch.StartNew();
// Ensure tree data exists (from E089_TreeCreate)
var root = await redb.TreeQuery<DepartmentProps>().WhereRoots().FirstOrDefaultAsync();
if (root == null)
{
sw.Stop();
return Fail("E176", "TreeQuery - DistinctBy", ExampleTier.Free, sw.ElapsedMilliseconds,
"No tree data found. Please run E089_TreeCreate first.");
}
// Get total count before distinct
var totalCount = await redb.TreeQuery<DepartmentProps>(root.Id).CountAsync();
// Get unique departments by IsActive status (will return max 2: true and false)
var query = redb.TreeQuery<DepartmentProps>(root.Id)
.DistinctBy(d => d.IsActive)
.Take(100);
// Uncomment to see generated SQL:
//var sql = query.ToSqlString();
//Console.WriteLine(sql);
var uniqueByStatus = await query.ToListAsync();
sw.Stop();
var statuses = uniqueByStatus.Select(d => $"IsActive={d.Props.IsActive}");
TreeDistinctByUniqueITreeQueryable.DistinctBy
MS Pro56ms(1)
PG Pro24ms(1)
TreeQuery - DistinctByRedb
Demonstrates DistinctByRedb on TreeQuery to get unique tree nodes by a base field. Uses _objects table fields directly (no EAV join) - highly optimized. Pro feature: DistinctByRedb with tree context.
var sw = Stopwatch.StartNew();
// Ensure tree data exists (from E089_TreeCreate)
var root = await redb.TreeQuery<DepartmentProps>().WhereRoots().FirstOrDefaultAsync();
if (root == null)
{
sw.Stop();
return Fail("E177", "TreeQuery - DistinctByRedb", ExampleTier.Free, sw.ElapsedMilliseconds,
"No tree data found. Please run E089_TreeCreate first.");
}
// Get total count before distinct
var totalCount = await redb.TreeQuery<DepartmentProps>(root.Id).CountAsync();
// Get unique departments by ParentId (base field) - shows unique parent branches
var query = redb.TreeQuery<DepartmentProps>(root.Id)
.DistinctByRedb(d => d.ParentId)
.Take(20);
// Uncomment to see generated SQL (no _values JOIN):
// var sql = query.ToSqlString();
// Console.WriteLine(sql);
var uniqueByParent = await query.ToListAsync();
sw.Stop();
var parentIds = uniqueByParent.Take(5).Select(d => d.parent_id?.ToString() ?? "null");
var sw = Stopwatch.StartNew();
// Ensure tree data exists (from E089_TreeCreate)
var root = await redb.TreeQuery<DepartmentProps>().WhereRoots().FirstOrDefaultAsync();
if (root == null)
{
sw.Stop();
return Fail("E178", "TreeQuery - OrderBy", ExampleTier.Free, sw.ElapsedMilliseconds,
"No tree data found. Please run E089_TreeCreate first.");
}
// Sort departments by Code (ascending)
var query = redb.TreeQuery<DepartmentProps>(root.Id)
.OrderBy(d => d.Code)
.Take(100);
// Uncomment to see generated SQL:
// var sql = query.ToSqlString();
// Console.WriteLine(sql);
var sorted = await query.ToListAsync();
sw.Stop();
var codes = sorted.Take(5).Select(d => d.Props.Code);
TreeOrderBySortAscendingITreeQueryable.OrderBy
MS130ms(100)
MS Pro54ms(100)
PG60ms(100)
PG Pro24ms(100)
TreeQuery - OrderByDescending
Demonstrates OrderByDescending on TreeQuery to sort tree nodes in reverse order. Descending order by Budget - highest budgets first. Pro feature: OrderByDescending with tree context.
Demonstrates combining DistinctBy and OrderByDescending on TreeQuery. Gets unique values by one field, sorted by another. Pro feature: chained tree query operations.
var sw = Stopwatch.StartNew();
// Ensure tree data exists (from E089_TreeCreate)
var root = await redb.TreeQuery<DepartmentProps>().WhereRoots().FirstOrDefaultAsync();
if (root == null)
{
sw.Stop();
return Fail("E180", "TreeQuery - DistinctBy + OrderBy", ExampleTier.Free, sw.ElapsedMilliseconds,
"No tree data found. Please run E089_TreeCreate first.");
}
// Get unique by IsActive, then sort by Budget descending
var query = redb.TreeQuery<DepartmentProps>(root.Id)
.Where(d => d.Budget > 0)
.DistinctBy(d => d.IsActive)
.OrderByDescending(d => d.Budget)
.Take(100);
// Uncomment to see generated SQL:
//var sql = query.ToSqlString();
//Console.WriteLine(sql);
var results = await query.ToListAsync();
sw.Stop();
var output = results.Select(d =>
$"Active={d.Props.IsActive}, Budget=${d.Props.Budget:N0}");