Esempio n. 1
0
 /// <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 virtual IEnumerable <T> ExecuteTable <T>(T itemOftype)
 {
     return(ExecuteInternalCommand(() =>
     {
         using (DbDataReader dr = this.Command.ExecuteReader())
         {
             if (TypeExtension.IsPrimitive(typeof(T)))
             {
                 return DataReaderConvertor.ToPrimitives <T>(dr);
             }
             else
             {
                 return DataReaderConvertor.ToAnonymous <T>(dr);
             }
         }
     }));
 }
Esempio n. 2
0
        /// <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 virtual T ExecuteRow <T>(T itemOftype)
        {
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                return(this.ExecuteScalar <T>());
            }
            else
            {
                // Get DataTable
                var rows = ExecuteInternalCommand(() =>
                {
                    using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        return(DataReaderConvertor.ToAnonymous <T>(dr));
                    }
                });

                // Return
                return(rows?.Any() == true?rows.First() : default(T));
            }
        }