Exemple #1
0
        /// <summary>
        /// ToList
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="reader">reader</param>
        /// <returns>T</returns>
        public static List <T> ToList <T>(this IDataReader reader) where T : class, new()
        {
            var result = new List <T>();

            DataTable dt = reader.GetSchemaTable();

            try
            {
                while (reader.Read())
                {
                    var t = new T();

                    if (dt != null)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            // 当前列名&属性名
                            string       columnName = dr[0].ToString();
                            PropertyInfo pro        = typeof(T).GetProperty(columnName);

                            if (pro == null)
                            {
                                continue;
                            }

                            if (!pro.CanWrite)
                            {
                                continue;
                            }

                            pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null);
                        }
                    }

                    result.Add(t);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (!reader.IsClosed)
                {
                    reader.Dispose();
                }
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// ToList
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="dt">dt</param>
        /// <returns>T</returns>
        public static List <T> ToList <T>(this DataTable dt) where T : class, new()
        {
            var result = new List <T>();

            foreach (DataRow dr in dt.Rows)
            {
                var t = new T();
                try
                {
                    foreach (DataColumn column in dt.Columns)
                    {
                        // 当前列名&属性名
                        string       columnName = column.ColumnName;
                        PropertyInfo pro        = typeof(T).GetProperty(columnName);

                        if (pro == null)
                        {
                            continue;
                        }

                        if (!pro.CanWrite)
                        {
                            continue;
                        }

                        pro.SetValue(t, ConvertExtension.ConvertHelper(dr[columnName], pro.PropertyType), null);
                    }
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }

                result.Add(t);
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// ToModel
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="dt">dt</param>
        /// <returns>T</returns>
        public static T ToModel <T>(this DataTable dt) where T : class, new()
        {
            var t = new T();

            if (dt.Rows.Count <= 0)
            {
                return(t);
            }

            try
            {
                foreach (DataColumn column in dt.Columns)
                {
                    // 当前列名&属性名
                    string       columnName = column.ColumnName;
                    PropertyInfo pro        = typeof(T).GetProperty(columnName);
                    if (pro == null)
                    {
                        continue;
                    }

                    if (!pro.CanWrite)
                    {
                        continue;
                    }

                    pro.SetValue(t, ConvertExtension.ConvertHelper(dt.Rows[0][columnName], pro.PropertyType), null);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

            return(t);
        }