Skip to content

Commit a2d2c4c

Browse files
author
electricessence
committed
Added SqlDataReader.ResultsAsync methods.
1 parent 862b51a commit a2d2c4c

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

Open.Database.Extensions.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<Description>Useful set of utilities and abstractions for simplifying modern database operations and ensuring DI compatibility.</Description>
1212
<RepositoryUrl>https://github.com/electricessence/Open.Database.Extensions</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
14-
<Version>5.2.0</Version>
15-
<AssemblyVersion>5.2.0.0</AssemblyVersion>
16-
<FileVersion>5.2.0.0</FileVersion>
14+
<Version>5.2.1</Version>
15+
<AssemblyVersion>5.2.1.0</AssemblyVersion>
16+
<FileVersion>5.2.1.0</FileVersion>
1717
<PackageReleaseNotes>- More extensions and flexibility for transforming results.</PackageReleaseNotes>
1818
</PropertyGroup>
1919

SqlClient/Extensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,42 @@ public static Task<QueryResult<Queue<object[]>>> RetrieveAsync(this SqlDataReade
286286
=> RetrieveAsync(reader, new string[1] { c }.Concat(others));
287287

288288

289+
/// <summary>
290+
/// Asynchronously returns all records and iteratively attempts to map the fields to type T.
291+
/// </summary>
292+
/// <typeparam name="T">The model type to map the values to (using reflection).</typeparam>
293+
/// <param name="reader">The IDataReader to read results from.</param>
294+
/// <param name="fieldMappingOverrides">An override map of field names to column names where the keys are the property names, and values are the column names.</param>
295+
/// <returns>A task containing the list of results.</returns>
296+
public static async Task<IEnumerable<T>> ResultsAsync<T>(this SqlDataReader reader, IEnumerable<(string Field, string Column)> fieldMappingOverrides) where T : new()
297+
{
298+
var x = new Transformer<T>(fieldMappingOverrides);
299+
return x.AsDequeueingEnumerable(await reader.RetrieveAsync(x.ColumnNames));
300+
}
301+
302+
/// <summary>
303+
/// Asynchronously returns all records and iteratively attempts to map the fields to type T.
304+
/// </summary>
305+
/// <typeparam name="T">The model type to map the values to (using reflection).</typeparam>
306+
/// <param name="reader">The IDataReader to read results from.</param>
307+
/// <param name="fieldMappingOverrides">An override map of field names to column names where the keys are the property names, and values are the column names.</param>
308+
/// <returns>A task containing the list of results.</returns>
309+
public static Task<IEnumerable<T>> ResultsAsync<T>(this SqlDataReader reader, IEnumerable<KeyValuePair<string, string>> fieldMappingOverrides) where T : new()
310+
=> reader.ResultsAsync<T>(fieldMappingOverrides?.Select(kvp => (kvp.Key, kvp.Value)));
311+
312+
/// <summary>
313+
/// Asynchronously returns all records and iteratively attempts to map the fields to type T.
314+
/// </summary>
315+
/// <typeparam name="T">The model type to map the values to (using reflection).</typeparam>
316+
/// <param name="reader">The IDataReader to read results from.</param>
317+
/// <param name="fieldMappingOverrides">An override map of field names to column names where the keys are the property names, and values are the column names.</param>
318+
/// <returns>A task containing the list of results.</returns>
319+
public static async Task<IEnumerable<T>> ResultsAsync<T>(this SqlDataReader reader, params (string Field, string Column)[] fieldMappingOverrides) where T : new()
320+
{
321+
var x = new Transformer<T>(fieldMappingOverrides);
322+
return x.AsDequeueingEnumerable(await reader.RetrieveAsync(x.ColumnNames));
323+
}
324+
289325
/// <summary>
290326
/// Creates an ExpressiveSqlCommand for subsequent configuration and execution.
291327
/// </summary>

0 commit comments

Comments
 (0)