C#DataTable转List

方法一:

 /// <summary>
        /// DataTable转List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> DtToList<T>(this DataTable dt) where T : class, new()
        {
            //属性列表
            List<System.Reflection.PropertyInfo> ls_pro = new List<System.Reflection.PropertyInfo>();

            Type t = typeof(T);
            //把T里所有public属性字段和dt列名一样的放进ls_pro属性列表存放
            Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) ls_pro.Add(p); });
            //Array.ForEach<System.Reflection.PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.Contains(p.Name)) ls_pro.Add(p); });
            List<T> ls_t = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T ob = new T();
                //遍历出属性列表里字段在dt里的值(非空即为有值)
                ls_pro.ForEach(p => { if (dr[p.Name] != DBNull.Value) p.SetValue(ob, dr[p.Name], null); });
                ls_t.Add(ob);
            }
            return ls_t;
        }

方法二:

 /// <summary>
        /// Datable转List,可转T类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ToList<T>(this DataTable dt) where T:class,new()
        {
            Type t = typeof(T);
            System.Reflection.PropertyInfo[] pi = t.GetProperties();//读取T里public属性字段
            List<T> ls = new List<T>();//存放转换结果
            string tname = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                T entity = new T();
                foreach (var p in pi)
                {
                    tname = p.Name;
                    if (dt.Columns.Contains(tname))
                    {
                        if (!p.CanWrite) continue;
                        object value = dr[tname];
                        if (p.PropertyType == typeof(string))
                        {
                            p.SetValue(entity,value.ToString(),null);
                        }
                        else if (p.PropertyType == typeof(int) || p.PropertyType == typeof(int?))
                        {
                            p.SetValue(entity, (int)value, null);
                        }
                        else if (p.PropertyType == typeof(decimal) || p.PropertyType == typeof(decimal?))
                        {
                            p.SetValue(entity, (decimal)value, null);
                        }
                        else if (p.PropertyType == typeof(float) || p.PropertyType == typeof(float?))
                        {
                            p.SetValue(entity, (float)value, null);
                        }
                        else if (p.PropertyType == typeof(bool))
                        {
                            p.SetValue(entity, (bool)value, null);
                        }
                        else if (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTime?))
                        {
                            p.SetValue(entity, (DateTime)value, null);
                        }
                        else
                        {
                            p.SetValue(entity, value, null);
                        }
                    }
                }
                ls.Add(entity);
            }
            return ls;
        }

猜你喜欢

转载自blog.csdn.net/zhangshineng/article/details/83178520