Filter an ordered record collection with the same key value
- Last UpdatedApr 08, 2026
- 2 minute read
This scenario guides you through the steps required to filter an ordered record collection with the same key value of a key field of an analog table in the RealTime database (RTDB).
To identify the code associated with each step, see the code comments in the Example code section.
- GetKeyValueEqualRecordCollection methods can be called with a record number to specify the starting record to create the collection from.
- Built-in types can be used for different type key fields. For example, RealTimeStringField class.
To filter an ordered record collection with the same key value
In this example, the RealTimeBoolField"commisioningRequired" with a value of "true" is used to filter.
- Open the Program.cs file from the created project.
- Open the RTDB.
- Access the analog table.
- Lock the analog table for reading.
- Lock the record for reading.
- Filter by key value.
- Process the ordered record collection.
- Unlock the record.
- Unlock the analog table.
Example code
IRealTimeDatabase database = new RealTimeDatabase();
try
{
// Open the RTDB.
if (!database.IsOpen())
{
database.Open();
}
// Access the analog table.
using (IRealTimeTable table = new RealTimeTable("analog"))
{
ITableLockRequestType lockRequest = new TableLockRequestType();
IRecordLockRequestType recordLockRequest = new RecordLockRequestType();
// Lock the analog table for reading.
table.Lock(lockRequest.SAFE_READ);
// Filter by key value.
RealTimeBoolField nameField = new RealTimeBoolField(table.Name, "commissioningRequired");
var orderCollection = table.GetKeyValueEqualRecordCollection(nameField, true);
// Process the ordered record collection.
foreach (var rtRecord in orderCollection)
{
// Lock the record for reading.
rtRecord.Lock(recordLockRequest.SAFE_READ);
Console.WriteLine(quot;Name: {rtRecord.ReadString("name")} has ptnum: {rtRecord.RecordNumber}");
rtRecord.UnLock();
} // Leaving scope releases the record lock.
} // Leaving scope releases the analog table lock.
}
catch (Exception ex)
{
Console.WriteLine(quot;Error occurred while getting the KeyValueEqualRecordCollection: {ex.Message} has been raised");
}