Path: redb/redb.Examples/Examples/E168_ArrayContains.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/E168_ArrayContains.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Demonstrates filtering by array element using Contains().
/// Server-side: = ANY(pvt."Skills") in PostgreSQL, STRING_SPLIT in MSSQL.
/// Pro feature: array operations in PVT.
///
[ExampleMeta("E168", "Array - Contains", "Query",
ExampleTier.Free, 168, "Array", "Contains", "Filter", RelatedApis = ["IRedbQueryable.Where", "Array.Contains"])]
public class E168_ArrayContains : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
// Find employees who have "C#" skill
var query = redb.Query()
.Where(e => e.Skills != null && e.Skills.Contains("C#"))
.Take(100);
// Uncomment to see generated SQL (contains ANY or STRING_SPLIT):
// var sql = await query.ToSqlStringAsync();
// Console.WriteLine(sql);
var results = await query.ToListAsync();
var totalCount = await redb.Query()
.Where(e => e.Skills != null && e.Skills.Contains("C#"))
.CountAsync();
sw.Stop();
var skillSamples = results.Take(3)
.Select(e => $"{e.Props.FirstName}: [{string.Join(", ", e.Props.Skills?.Take(3) ?? [])}]");
return Ok("E168", "Array - Contains", ExampleTier.Free, sw.ElapsedMilliseconds, totalCount,
[$"Filter: Skills.Contains(\"C#\")",
$"Found: {totalCount} employees",
$"Samples: {string.Join("; ", skillSamples)}"]);
}
}