/// <summary> /// Execute the query and return an array of new instances of typed results filled with data table result. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="converter"></param> /// <returns>Array of typed results</returns> public virtual IEnumerable <T> ExecuteTable <T>(Func <Schema.DataRow, T> converter) { var table = ExecuteInternalCommand(() => { using (DbDataReader dr = this.Command.ExecuteReader()) { return(DataReaderConvertor.ToDataTable(dr)); } }); if (table != null && table.Rows != null) { return(table.Rows.Select(row => converter.Invoke(row))); } else { return new T[] { } }; }
/// <summary> /// Execute the query and fill the specified T object with the first row of results /// </summary> /// <typeparam name="T">Object type</typeparam> /// <param name="converter"></param> /// <returns>First row of results</returns> public virtual T ExecuteRow <T>(Func <Schema.DataRow, T> converter) { if (TypeExtension.IsPrimitive(typeof(T))) { return(this.ExecuteScalar <T>()); } else { // Get DataRow var table = ExecuteInternalCommand(() => { using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.SingleRow)) { return(DataReaderConvertor.ToDataTable(dr)); } }); var row = table?.Rows?.FirstOrDefault(); // Return return(row != null?converter.Invoke(row) : default(T)); } }