Path: redb/redb.Examples/Examples/E117_ListItemWithObject.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/E117_ListItemWithObject.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Creates a ListItem with linked RedbObject.
/// ListItem can reference any object via IdObject property.
/// Linked object is loaded lazily via Object property.
/// Requires E114+E115 to run first.
///
[ExampleMeta("E117", "ListItem - With Object", "List",
ExampleTier.Free, 117, "ListItem", "Object", "Reference", RelatedApis = ["IListProvider.SaveListItemAsync", "RedbListItem.Object"])]
public class E117_ListItemWithObject : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Get existing list
var list = await redb.ListProvider.GetListByNameAsync("ExampleStatuses");
if (list == null)
{
sw.Stop();
return Fail("E117", "ListItem - With Object", ExampleTier.Free, sw.ElapsedMilliseconds,
"List 'ExampleStatuses' not found. Run E114 first.");
}
// Create a city object
var cityObj = new RedbObject
{
name = "Moscow",
Props = new CityProps
{
Name = "Moscow",
Population = 12_655_000,
Region = "Central Federal District",
IsCapital = true,
Coordinates = [55.7558, 37.6173]
}
};
var cityId = await redb.SaveAsync(cityObj);
// Create a new list item with object reference
var item = new RedbListItem(list, "Capital", "Capital city status", cityObj);
// Uncomment to see generated SQL:
// (Lists use direct ADO.NET, no ORM query)
var savedItem = await redb.ListProvider.SaveListItemAsync(item);
sw.Stop();
return Ok("E117", "ListItem - With Object", ExampleTier.Free, sw.ElapsedMilliseconds, 1,
[$"Item: {savedItem.Value} (ID: {savedItem.Id})", $"Linked object ID: {savedItem.IdObject}"]);
}
}