Примеры кода

Изучайте RedBase на примерах. Просматривайте рабочие примеры кода для LINQ-запросов, массовых операций, древовидных структур и многого другого.

Data Model - EmployeeProps Нажмите, чтобы увидеть модель данных, используемую во всех примерах
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; }
}

Array Contains - Multiple OR

Query employees with multiple skills using Array.Contains with OR.
Find developers who know C# OR Python.
var sw = Stopwatch.StartNew();

// Pro: Multiple array Contains with OR
var query = redb.Query<EmployeeProps>()
    .Where(e => e.Skills!.Contains("C#") || e.Skills!.Contains("Python"))
    .Take(100);

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
ArrayContainsORPro
MS Pro 159ms(100)
mssql.pro.cache 267ms(100)
mssql.pro.repeat 238ms(100)
postgres.ef 1101ms(100)
postgres.ef.repeat 1179ms(100)
PG Pro 95ms(100)
postgres.pro.cache 25ms(100)
postgres.pro.repeat 51ms(100)
sqlite.pro 48ms(100)
sqlite.pro.cache 11ms(100)
sqlite.pro.repeat 11ms(100)

Array NOT Contains

Query employees excluding specific skill.
Find developers who don't have "intern" skill.
var sw = Stopwatch.StartNew();

// Pro: NOT Contains - exclude employees with specific skill
var query = redb.Query<EmployeeProps>()
    .Where(e => e.Skills!.Contains("C#") && !e.Skills!.Contains("intern"))
    .Take(100);

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
ArrayContainsNOTPro
MS Pro 153ms(100)
mssql.pro.cache 180ms(100)
mssql.pro.repeat 176ms(100)
postgres.ef 936ms(100)
postgres.ef.repeat 1084ms(100)
PG Pro 209ms(100)
postgres.pro.cache 130ms(100)
postgres.pro.repeat 138ms(100)
sqlite.pro 42ms(100)
sqlite.pro.cache 12ms(100)
sqlite.pro.repeat 15ms(100)

Array Contains + Conditions

Query employees combining array Contains with other conditions (Pro).
Find senior C# developers over 30.
var sw = Stopwatch.StartNew();

// Pro: Combine array Contains with other conditions
var query = redb.Query<EmployeeProps>()
    .Where(e => e.Skills!.Contains("C#") && e.Age >= 30 && e.Salary > 70000m)
    .Take(100);

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
ArrayContainsANDPro
MS Pro 204ms(100)
mssql.pro.cache 139ms(100)
mssql.pro.repeat 171ms(100)
postgres.ef 868ms(100)
postgres.ef.repeat 1002ms(100)
PG Pro 104ms(100)
postgres.pro.cache 30ms(100)
postgres.pro.repeat 54ms(100)
sqlite.pro 215ms(100)
sqlite.pro.cache 170ms(100)
sqlite.pro.repeat 187ms(100)

Dict ContainsKey - Phone Directory

Query employees using Dictionary.ContainsKey.
Find employees who have a desk phone.
var sw = Stopwatch.StartNew();

// Pro: Query dictionary with ContainsKey
var query = redb.Query<EmployeeProps>()
    .Where(e => e.PhoneDirectory!.ContainsKey("desk"))
    .Take(100);

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
DictionaryContainsKeyPro
MS Pro 83ms(100)
mssql.pro.cache 17ms(100)
mssql.pro.repeat 22ms(100)
postgres.ef 1099ms(100)
postgres.ef.repeat 1031ms(100)
PG Pro 89ms(100)
postgres.pro.cache 17ms(100)
postgres.pro.repeat 15ms(100)
sqlite.pro 41ms(100)
sqlite.pro.cache 8ms(100)
sqlite.pro.repeat 11ms(100)

Dict Indexer - Bonus by Year

Query employees using Dictionary indexer.
Find employees with bonus over 6000 in 2023.
var sw = Stopwatch.StartNew();

// Pro: Query dictionary with indexer
var query = redb.Query<EmployeeProps>()
    .Where(e => e.BonusByYear![2023] > 6000m)
    .Take(100);

// Uncomment to see generated SQL:
//var sql = await query.ToSqlStringAsync();
//Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
DictionaryIndexerPro
MS Pro 116ms(100)
mssql.pro.cache 80ms(100)
mssql.pro.repeat 83ms(100)
postgres.ef 1009ms(100)
postgres.ef.repeat 1027ms(100)
PG Pro 94ms(100)
postgres.pro.cache 21ms(100)
postgres.pro.repeat 17ms(100)
sqlite.pro 42ms(100)
sqlite.pro.cache 27ms(100)
sqlite.pro.repeat 34ms(100)

Dict - Nested Class Property

Query employees using Dict indexer with nested class (Pro).
Find employees whose HQ office is in specific city.
var sw = Stopwatch.StartNew();

// Pro: Dictionary<string, Address> with nested property access
var query = redb.Query<EmployeeProps>()
    .Where(e => e.OfficeLocations!["HQ"].City == "New York")
    .Take(100);

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
DictionaryNestedClassPro
MS Pro 180ms(100)
mssql.pro.cache 116ms(100)
mssql.pro.repeat 112ms(100)
postgres.ef 985ms(100)
postgres.ef.repeat 1017ms(100)
PG Pro 85ms(100)
postgres.pro.cache 20ms(100)
postgres.pro.repeat 17ms(100)
sqlite.pro 42ms(100)
sqlite.pro.cache 11ms(100)
sqlite.pro.repeat 17ms(100)

Dict - Tuple Key

Query employees using Dict with tuple key (Pro).
Find employees with specific performance review.
var sw = Stopwatch.StartNew();

// Pro: Dictionary<(int Year, string Quarter), string> with tuple key
// Tuple key must be in a variable for Expression Tree to work
var reviewKey = (Year: 2024, Quarter: "Q1");

var query = redb.Query<EmployeeProps>()
    .Where(e => e.PerformanceReviews![reviewKey] == "Excellent")
    .Take(100);

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var result = await query.ToListAsync();
sw.Stop();
DictionaryTupleKeyPro
MS Pro 81ms(100)
mssql.pro.cache 50ms(100)
mssql.pro.repeat 44ms(100)
postgres.ef 937ms(100)
postgres.ef.repeat 1042ms(100)
PG Pro 86ms(100)
postgres.pro.cache 39ms(100)
postgres.pro.repeat 41ms(100)
sqlite.pro 43ms(100)
sqlite.pro.cache 12ms(100)
sqlite.pro.repeat 12ms(100)

CountAsync - Total and Filtered

Count employees using CountAsync.
Get total count and filtered count.
var sw = Stopwatch.StartNew();

// Total count
var totalCount = await redb.Query<EmployeeProps>().CountAsync();

// Filtered count
var managersCount = await redb.Query<EmployeeProps>()
    .Where(e => e.Position == "Manager")
    .CountAsync();

// Filtered by a field two nested classes deep
var highFloorCount = await redb.Query<EmployeeProps>()
    .Where(e => e.HomeAddress!.Building!.Floor > 20)
    .CountAsync();

sw.Stop();
CountAggregate
MS Pro 116ms(100207)
mssql.pro.cache 115ms(100209)
mssql.pro.repeat 117ms(100208)
postgres.ef 97ms(100306)
postgres.ef.repeat 73ms(100307)
PG Pro 82ms(100307)
postgres.pro.cache 73ms(100309)
postgres.pro.repeat 73ms(100308)
sqlite.pro 47ms(100101)
sqlite.pro.cache 43ms(100106)
sqlite.pro.repeat 37ms(100105)

AnyAsync - Check Existence

Check if any employees exist using AnyAsync().
Returns true if at least one record exists.
var sw = Stopwatch.StartNew();

// Check if any employees exist (no predicate)
var query = redb.Query<EmployeeProps>();

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var anyExist = await query.AnyAsync();

// Existence within a set filtered by a field two nested classes deep
var anyOnTopFloor = await redb.Query<EmployeeProps>()
    .Where(e => e.HomeAddress!.Building!.Floor == 30)
    .AnyAsync();

sw.Stop();
AnyExistsIRedbQueryable.AnyAsync
MS Pro 26ms(1)
mssql.pro.cache 25ms(1)
mssql.pro.repeat 22ms(1)
postgres.ef 23ms(1)
postgres.ef.repeat 17ms(1)
PG Pro 24ms(1)
postgres.pro.cache 21ms(1)
postgres.pro.repeat 21ms(1)
sqlite.pro 6ms(1)
sqlite.pro.cache 2ms(1)
sqlite.pro.repeat 2ms(1)

AnyAsync - With Predicate

Check if any employees match a condition using AnyAsync(predicate).
More efficient than Where().AnyAsync() - stops at first match.
var sw = Stopwatch.StartNew();

// Check if any high earners exist (salary > 100k)
var query = redb.Query<EmployeeProps>();

// Uncomment to see generated SQL:
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);

var anyHighEarners = await query.AnyAsync(e => e.Salary > 100_000m);

// Predicate over a field one nested class deep and a field two nested classes deep
var anyBerlinHighFloor = await query.AnyAsync(e => e.HomeAddress!.City == "Berlin" && e.HomeAddress!.Building!.Floor > 25);

sw.Stop();
AnyPredicateFilterIRedbQueryable.AnyAsync
MS Pro 13ms(1)
mssql.pro.cache 12ms(1)
mssql.pro.repeat 14ms(1)
postgres.ef 20ms(1)
postgres.ef.repeat 18ms(1)
PG Pro 20ms(1)
postgres.pro.cache 18ms(1)
postgres.pro.repeat 18ms(1)
sqlite.pro 4ms(1)
sqlite.pro.cache 4ms(1)
sqlite.pro.repeat 3ms(1)