Exemplo n.º 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();
                    reader.Close();
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将DataTable 转换成list 字段对照方式
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="dt">dt</param>
        /// <param name="field">转换字典</param>
        /// <returns>T</returns>
        public static List <T> ToList <T>(this DataTable dt, Dictionary <string, string> field) 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        = null;
                        if (field.ContainsKey(columnName))
                        {
                            pro = typeof(T).GetProperty(field[columnName]);
                        }
                        else
                        {
                            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);
        }
Exemplo n.º 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);
        }