Skip to content

ToDictionary

Returns a DataTableDictionary indexed by the specified columns for fast access.

1
myDataTable.ToDictionary(columns: string, separator?: string):DataTableDictionary

Parameters

string columns
    A comma-separated or semicolon-separated list of the key columns. Required.

string separator optional
    The separator between column values when several columns are specified. Defaults to ..

Returns

A DataTableDictionary instance.

Remarks

An error is thrown when a column does not exist in the table.

Keys are compared case-insensitively. If several rows share a key, the dictionary returns an array of rows for that key. When a key contains no . character, leading zeroes are removed; to keep significant leading zeroes, use a key format that contains a . character.

Use a dictionary instead of querying the database inside a loop.

Examples

Index rows by a column

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var employees = $Database.Get({
    TargetSchema : 'HR',
    TargetTable : 'Employee'
}).ToDictionary('RegistryNumber');

// {
//   "1234" : { Id : "...", RegistryNumber : "1234", ... },
//   "4567" : { Id : "...", RegistryNumber : "4567", ... }
// }

var employee = employees.Get('1234');

Index rows by several columns

1
2
3
4
5
6
var prices = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Prices'
}).ToDictionary('ProductCode,Currency', '|');

var price = prices.Get('P-100|EUR');

See Also