| | 1 | | using System.Data; |
| | 2 | |
|
| | 3 | | namespace Songhay.Extensions; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Extensions of <see cref="DataSet"/>. |
| | 7 | | /// </summary> |
| | 8 | | public static class DataSetExtensions |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Converts the <see cref="DataSet"/> into a first table data rows. |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="dataSet">The data set.</param> |
| | 14 | | public static IEnumerable<DataRow> ToFirstTableDataRows(this DataSet? dataSet) |
| 0 | 15 | | { |
| 0 | 16 | | if (dataSet == null) return Enumerable.Empty<DataRow>(); |
| | 17 | |
|
| 0 | 18 | | var table = dataSet |
| 0 | 19 | | .Tables.OfType<DataTable>() |
| 0 | 20 | | .FirstOrDefault(); |
| | 21 | |
|
| 0 | 22 | | return table == null ? Enumerable.Empty<DataRow>() : table.Rows.OfType<DataRow>(); |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Converts the <see cref="DataSet"/> into a first table first column. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="dataSet">The data set.</param> |
| | 29 | | public static IEnumerable<string> ToFirstTableFirstColumn(this DataSet? dataSet) => |
| 0 | 30 | | dataSet.ToFirstTableFirstColumn<string>(); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Converts the <see cref="DataSet"/> into a first table first column. |
| | 34 | | /// </summary> |
| | 35 | | /// <typeparam name="TColumn">The type of the column.</typeparam> |
| | 36 | | /// <param name="dataSet">The data set.</param> |
| | 37 | | public static IEnumerable<TColumn> ToFirstTableFirstColumn<TColumn>(this DataSet? dataSet) |
| 0 | 38 | | { |
| 0 | 39 | | var rows = dataSet.ToFirstTableDataRows(); |
| | 40 | |
|
| 0 | 41 | | return rows.Select(i => (TColumn) i[0]); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Converts the <see cref="DataSet"/> into a first table rows. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="dataSet">The data set.</param> |
| | 48 | | public static IEnumerable<KeyValuePair<string, string>> ToFirstTableRows(this DataSet? dataSet) => |
| 0 | 49 | | dataSet.ToFirstTableRows<string, string>(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Converts the <see cref="DataSet"/> into a first table rows. |
| | 53 | | /// </summary> |
| | 54 | | /// <typeparam name="TKey">The type of the key.</typeparam> |
| | 55 | | /// <typeparam name="TValue">The type of the value.</typeparam> |
| | 56 | | /// <param name="dataSet">The data set.</param> |
| | 57 | | public static IEnumerable<KeyValuePair<TKey, TValue>> ToFirstTableRows<TKey, TValue>(this DataSet? dataSet) |
| 0 | 58 | | { |
| 0 | 59 | | var rows = dataSet.ToFirstTableDataRows(); |
| | 60 | |
|
| 0 | 61 | | return rows.Select(i => new KeyValuePair<TKey, TValue>((TKey) i[0], (TValue) i[1])); |
| 0 | 62 | | } |
| | 63 | | } |