Method ToDictionary
- Namespace
- Open.Database.Extensions.Synchronous
- Assembly
- Open.Database.Extensions.Core.dll
ToDictionary<T, TKey>(MappedQuery<T>, Func<T, TKey>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>, keyed by keySelector.
public static Dictionary<TKey, T> ToDictionary<T, TKey>(this MappedQuery<T> query, Func<T, TKey> keySelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedQuery<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, T>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
Remarks
All rows are buffered in memory before the call returns.
The built-in queries read every row first and add them after the last one, so a duplicate key (or a null key) throws only after the whole read.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
queryorkeySelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToList()
ToDictionary<T, TKey, TValue>(MappedQuery<T>, Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>,
keyed by keySelector, with the value
valueSelector returns for the row.
public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this MappedQuery<T> query, Func<T, TKey> keySelector, Func<T, TValue> valueSelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedQuery<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
valueSelectorFunc<T, TValue>Produces the value for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, TValue>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
TValueThe type of the dictionary value.
Remarks
Each selector is called once per row, key first.
The built-in queries read every row first and build the dictionary right after the last one, while the reader is still open:
a duplicate key (or a null key) throws, and the selectors run, only then, as it does for ToDictionary<TKey>(Func<T, TKey>, IEqualityComparer<TKey>) and for LINQ's ToDictionary.
Keep the selectors quick and do not touch the connection from inside them.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
query,keySelectororvalueSelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToDictionaryAsync<TKey, TValue>(Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>, CancellationToken)
ToDictionary<T, TKey>(MappedQueryWithSkip<T>, Func<T, TKey>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>, keyed by keySelector.
public static Dictionary<TKey, T> ToDictionary<T, TKey>(this MappedQueryWithSkip<T> query, Func<T, TKey> keySelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedQueryWithSkip<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, T>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
Remarks
All rows are buffered in memory before the call returns.
The built-in queries read every row first and add them after the last one, so a duplicate key (or a null key) throws only after the whole read.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
queryorkeySelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToList()
ToDictionary<T, TKey, TValue>(MappedQueryWithSkip<T>, Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>,
keyed by keySelector, with the value
valueSelector returns for the row.
public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this MappedQueryWithSkip<T> query, Func<T, TKey> keySelector, Func<T, TValue> valueSelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedQueryWithSkip<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
valueSelectorFunc<T, TValue>Produces the value for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, TValue>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
TValueThe type of the dictionary value.
Remarks
Each selector is called once per row, key first.
The built-in queries read every row first and build the dictionary right after the last one, while the reader is still open:
a duplicate key (or a null key) throws, and the selectors run, only then, as it does for ToDictionary<TKey>(Func<T, TKey>, IEqualityComparer<TKey>) and for LINQ's ToDictionary.
Keep the selectors quick and do not touch the connection from inside them.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
query,keySelectororvalueSelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToDictionaryAsync<TKey, TValue>(Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>, CancellationToken)
ToDictionary<T, TKey>(MappedQueryWithTake<T>, Func<T, TKey>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>, keyed by keySelector.
public static Dictionary<TKey, T> ToDictionary<T, TKey>(this MappedQueryWithTake<T> query, Func<T, TKey> keySelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedQueryWithTake<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, T>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
Remarks
All rows are buffered in memory before the call returns.
The built-in queries read every row first and add them after the last one, so a duplicate key (or a null key) throws only after the whole read.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
queryorkeySelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToList()
ToDictionary<T, TKey, TValue>(MappedQueryWithTake<T>, Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>,
keyed by keySelector, with the value
valueSelector returns for the row.
public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this MappedQueryWithTake<T> query, Func<T, TKey> keySelector, Func<T, TValue> valueSelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedQueryWithTake<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
valueSelectorFunc<T, TValue>Produces the value for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, TValue>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
TValueThe type of the dictionary value.
Remarks
Each selector is called once per row, key first.
The built-in queries read every row first and build the dictionary right after the last one, while the reader is still open:
a duplicate key (or a null key) throws, and the selectors run, only then, as it does for ToDictionary<TKey>(Func<T, TKey>, IEqualityComparer<TKey>) and for LINQ's ToDictionary.
Keep the selectors quick and do not touch the connection from inside them.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
query,keySelectororvalueSelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToDictionaryAsync<TKey, TValue>(Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>, CancellationToken)
ToDictionary<T, TKey>(MappedCommand<T>, Func<T, TKey>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>, keyed by keySelector.
public static Dictionary<TKey, T> ToDictionary<T, TKey>(this MappedCommand<T> query, Func<T, TKey> keySelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedCommand<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, T>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
Remarks
All rows are buffered in memory before the call returns.
The built-in queries read every row first and add them after the last one, so a duplicate key (or a null key) throws only after the whole read.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
queryorkeySelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToList()
ToDictionary<T, TKey, TValue>(MappedCommand<T>, Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>?)
Reads every mapped row into a new Dictionary<TKey, TValue>,
keyed by keySelector, with the value
valueSelector returns for the row.
public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this MappedCommand<T> query, Func<T, TKey> keySelector, Func<T, TValue> valueSelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull
Parameters
queryMappedCommand<T>The query to execute.
keySelectorFunc<T, TKey>Produces the key for each mapped row.
valueSelectorFunc<T, TValue>Produces the value for each mapped row.
comparerIEqualityComparer<TKey>The comparer for the keys, or null to use the default comparer.
Returns
- Dictionary<TKey, TValue>
A dictionary of all the rows. It is empty if there are no rows.
Type Parameters
TThe type each row is mapped to.
TKeyThe type of the dictionary key.
TValueThe type of the dictionary value.
Remarks
Each selector is called once per row, key first.
The built-in queries read every row first and build the dictionary right after the last one, while the reader is still open:
a duplicate key (or a null key) throws, and the selectors run, only then, as it does for ToDictionary<TKey>(Func<T, TKey>, IEqualityComparer<TKey>) and for LINQ's ToDictionary.
Keep the selectors quick and do not touch the connection from inside them.
Pooled memory: the rows are held in arrays rented from a pool and given
back when the call, the buffer or the stream is done.
By default that pool is ArrayPool<T>.Shared.
To use your own, call .UsePool(pool, clearOnReturn: ...) after
Map: the pool is yours, ODX rents from it and gives back to it,
and never disposes it.
The arrays it hands out may be longer than asked for: ODX uses the rows
it wrote and clears only those.
A pool that hands out a shorter array than asked for makes the read
throw InvalidOperationException.
Returning an array must not throw.
Clearing: rows that hold references (a class, a string, a struct that
contains either) are always cleared before their array goes back, so a
pooled array never keeps a row alive; nothing turns that off.
Rows with no references (an int, a plain struct) go back as they
are, unless asked:
UsePool(pool, clearOnReturn: true) clears them,
clearOnReturn: false keeps them as they are whatever the app-wide setting
says, and a query that does not call UsePool follows the setting.
The app-wide setting is the AppContext switch
Open.Database.Extensions.ClearPooledArrays (off by default; read
once, so set it before the first query).
Set it in the application's project file with
<RuntimeHostConfigurationOption
Include="Open.Database.Extensions.ClearPooledArrays" Value="true"
/>, which puts it in runtimeconfig.json, or call
AppContext.SetSwitch before the first query.
Exceptions
- ArgumentNullException
query,keySelectororvalueSelectoris null.- ArgumentException
Two rows produce the same key.
- See Also
-
ToDictionaryAsync<TKey, TValue>(Func<T, TKey>, Func<T, TValue>, IEqualityComparer<TKey>, CancellationToken)