/// <summary> /// Gets the first value of the first table of the first row. /// </summary> /// <param name="records">The source sequence to get the value from.</param> /// <returns>The value converted to the specified type.</returns> /// <remarks>Additional rows, tables and columns are ignored.</remarks> /// <exception cref="InvalidOperationException">Thrown if there is not at least one value to read from the supplied records.</exception> public static object GetScalar(this IEnumerable <Record> records) { if (records == null) { throw new ArgumentNullException(nameof(records)); } // Get the single row Record row = records.FirstOrDefault(); if (row == null) { throw new InvalidOperationException($"To get a scalar value the {nameof(Record)} sequence must contain at least one row."); } // Get the single table RecordItem item = row.Values.FirstOrDefault(); if (item == null) { throw new InvalidOperationException($"To get a scalar value the {nameof(Record)} sequence must contain at least one table on its first row."); } // Check there is a column to read from if (item.Count == 0) { throw new InvalidOperationException($"To get a scalar value the {nameof(Record)} sequence must contain at least one column on the first table on its first row."); } // Get the value from the item return(item.Values.FirstOrDefault()); }
private static RecordItem ReadItem(this DbDataReader reader, IEnumerable <CommandField> fields, string type) { RecordItem item = new RecordItem(type); foreach (CommandField field in fields) { object value = reader[field.Ordinal]; if (value is DBNull) { value = null; } item[field.FieldName] = value; } return(item); }