/// <summary> /// Executes query and projects each record into a new form. /// </summary> /// <typeparam name="T">Type of the output collection.</typeparam> /// <param name="select">Source <see cref="Select"/> to read data.</param> /// <param name="readMethod">Record transformation method.</param> /// <returns> /// An <see cref="IEnumerable{T}"/> whose elements are the result of invoking the transform /// function on each line read. /// </returns> public static IEnumerable <T> ExecuteEnumerable <T>(this Select select, ExecuteReaderTypedReadMethod <T> readMethod) { using (DBExecutor dbExecutor = EnsureSpecialDbExecutor(select.UserConnection, select.QueryKind)) { using (IDataReader dataReader = select.ExecuteReader(dbExecutor)) { while (dataReader.Read()) { yield return(readMethod(dataReader)); } } } }
/// <summary> /// Reads single record and applies transformation on it. /// </summary> /// <typeparam name="T">Type of the output item.</typeparam> /// <param name="select">Source <see cref="Select"/> to read data.</param> /// <param name="readMethod">Record transformation method.</param> /// <param name="item">Read and transformed item, or default <see cref="T"/> if nothing was read.</param> /// <returns>Returns <c>true</c> if record was read, <c>false</c> otherwise.</returns> /// <example> /// This sample shows how to call the <see cref="ExecuteSingleRecord{T}"/> method. /// <code> /// select.ExecuteSingleRecord(reader => new { /// Id = reader.GetColumnValue<Guid>("Id"), /// Name = reader.GetColumnValue<string>("Name") /// }, out var item); /// </code> /// </example> public static bool ExecuteSingleRecord <T>(this Select select, ExecuteReaderTypedReadMethod <T> readMethod, out T item) { using (DBExecutor dbExecutor = EnsureSpecialDbExecutor(select.UserConnection, select.QueryKind)) { using (IDataReader dataReader = select.ExecuteReader(dbExecutor)) { if (dataReader.Read()) { item = readMethod(dataReader); return(true); } item = default(T); return(false); } } }