/// <summary> /// Execute the query and return an array of new instances of typed results filled with data table result. /// </summary> /// <typeparam name="T">Object type</typeparam> /// <param name="itemOftype"></param> /// <returns>Array of typed results</returns> /// <example> /// <code> /// Employee emp = new Employee(); /// var x = cmd.ExecuteTable(new { emp.Age, emp.Name }); /// var y = cmd.ExecuteTable(new { Age = 0, Name = "" }); /// </code> /// <remarks> /// Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute /// to set which column name (ex. [Column("Name")] must be associated to this property. /// </remarks> /// </example> public async virtual Task <IEnumerable <T> > ExecuteTableAsync <T>(T itemOftype) { return(await ExecuteInternalCommand(async() => { using (DbDataReader dr = await this.Command.ExecuteReaderAsync()) { if (TypeExtension.IsPrimitive(typeof(T))) { return await DataReaderConvertor.ToPrimitivesAsync <T>(dr); } else { return await DataReaderConvertor.ToAnonymousAsync <T>(dr); } } })); }
/// <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="itemOftype"></param> /// <returns>First row of results</returns> /// <example> /// <code> /// Employee emp = new Employee(); /// var x = cmd.ExecuteRow(new { emp.Age, emp.Name }); /// var y = cmd.ExecuteRow(new { Age = 0, Name = "" }); /// var z = cmd.ExecuteRow(emp); /// </code> /// <remarks> /// Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute /// to set which column name (ex. [Column("Name")] must be associated to this property. /// </remarks> /// </example> public async virtual Task <T> ExecuteRowAsync <T>(T itemOftype) { if (TypeExtension.IsPrimitive(typeof(T))) { return(await this.ExecuteScalarAsync <T>()); } else { // Get DataTable var rows = await ExecuteInternalCommand(async() => { using (DbDataReader dr = await this.Command.ExecuteReaderAsync(System.Data.CommandBehavior.SingleRow)) { return(await DataReaderConvertor.ToAnonymousAsync <T>(dr)); } }); // Return return(rows?.Any() == true?rows.First() : default(T)); } }