C#DataTable转List

方法一:

 ///


        /// DataTable转List
        ///

        ///
        ///
        ///
        public static List DtToList(this DataTable dt) where T : class, new()
        {
            //属性列表
            List ls_pro = new List();

            Type t = typeof(T);
            //把T里所有public属性字段和dt列名一样的放进ls_pro属性列表存放
            Array.ForEach(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) ls_pro.Add(p); });
            //Array.ForEach(t.GetProperties(), p => { if (dt.Columns.Contains(p.Name)) ls_pro.Add(p); });
            List ls_t = new List();
            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;
        }

 

方法二:

 ///


        /// Datable转List,可转T类型
        ///

        ///
        ///
        ///
        public static List ToList(this DataTable dt) where T:class,new()
        {
            Type t = typeof(T);
            System.Reflection.PropertyInfo[] pi = t.GetProperties();//读取T里public属性字段
            List ls = new List();//存放转换结果
            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;
        }

你可能感兴趣的:(C#DataTable转List)