Path: redb/redb.Examples/Examples/E002_LoadAsync.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/Examples/E002_LoadAsync.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Example: Load employee by ID.
///
[ExampleMeta("E002", "LoadAsync - Load Employee by ID", "CRUD",
ExampleTier.Free, 1, "LoadAsync", "Read", "CRUD")]
public class E002_LoadAsync : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Get any existing employee ID
var query = redb.Query().Take(1);
var items = await query.ToListAsync();
if (items.Count == 0)
{
sw.Stop();
return Fail("E002", "LoadAsync - Load Employee by ID", ExampleTier.Free, sw.ElapsedMilliseconds,
"No employees found. Run E000 first.");
}
var id = items[0].Id;
var loaded = await redb.LoadAsync(id);
if (loaded != null)
{
EmployeeProps? employee = (EmployeeProps)loaded;
}
sw.Stop();
return Ok("E002", "LoadAsync - Load Employee by ID", ExampleTier.Free, sw.ElapsedMilliseconds,
[$"Loaded ID: {loaded.Id}", $"Employee: {loaded.Props?.FirstName} {loaded.Props?.LastName}, {loaded.Props?.Position}"]);
}
}