Skip to content

ToDictionary

Returns a DataTableDictionary by specified columns as key value to fast accessing.

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

Parameters

string columns
    Comma or semicolon separated column names. If not specified error is thrown.

string separator
    Separator to use as dictionary key when multiple columns specified. If not specified "." value is used by default.

Remarks

When columns argument contains an invalid column which is not in DataTable error is thrown.
If multiple columns shares same key, dictionary value would be array of data table row.

Convert users to a dictionary

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var employeeTable = $Database.Get({
    Parameters : {
        TargetSchema : 'HR',
        TargetTable : 'Employee'
    }
});

var employeeList = employeeTable.ToDictionary('RegistryNumber');

// employeeList now contains
// {
//    "1234" : { Id : "ABC", RegistryNumber: "1234", StartDate: ... }
//    "4567" : { Id : "DEF", RegistryNumber: "4567", StartDate: ... }
// }

var employee = employeeList.Get("12345");

Using multiple columns

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var employeeTable = $Database.Get({
    Parameters : {
        TargetSchema : 'HR',
        TargetTable : 'Employee'
    }
});

var employeeList = employeeTable.ToDictionary('Id,RegistryNumber');

// employeeList now contains
// {
//    "ABC.1234" : { Id : "ABC", RegistryNumber: "1234", StartDate: ... }
//    "DEF.4567" : { Id : "DEF", RegistryNumber: "4567", StartDate: ... }
// }

var employee = employeeList.Get("ABC.1234");

See Also