Retrieve an ordered or reversed record collection sorted by key
- Last UpdatedApr 08, 2026
- 2 minute read
This scenario guides you through the steps required to retrieve an ordered or reversed record collection sorted by key for 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.
- GetKeyRercordOrderCollection methods can be called with a record number to specify the starting record to create the collection from.
- To get the record collection in reverse order, use the GetReverseKeyOrderRecordCollection method and the appropriate ReverseKeyOrderRecordCollection class.
- ReverseKeyOrderRecordCollection is directly accessible without the use of the HPDBKeyOrderRecordCollection property
To retrieve an ordered record collection sorted by key
In this example, the RealTimeField key is used.
- 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.
- Get an ordered record collection sorted by key.
- Process the ordered record collection sorted by key.
- 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);
// Get the ordered record collection sorted by key.
var orderCollection = table.GetKeyOrderRecordCollection(new RealTimeField(table.Name, "name"));
// Process the ordered record collection sorted by key.
foreach (var rtRecord in orderCollection.HPDBKeyOrderRecordCollection)
{
// 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 KeyOrderRecordCollection: {ex.Message} has been raised");
}