DataTable 转换成List

工具类

	/// <summary>
    ///     数据表转换类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DbTableToList<T> where T : new()
    {
        public List<T> ConvertToList(DataTable dt)
        {
            // 定义集合  
            var list = new List<T>();
            if (0 == dt.Rows.Count)
            {
                return list;
            }
            // 获得此模型的可写公共属性  
            IEnumerable<PropertyInfo> propertys = new T().GetType().GetProperties().Where(u => u.CanWrite);
            list = ConvertToEntity(dt, propertys);
            return list;
        }
        
        private List<T> ConvertToEntity(DataTable dt, IEnumerable<PropertyInfo> propertys)
        {
            var list = new List<T>();
            //遍历DataTable中所有的数据行  
            foreach (DataRow dr in dt.Rows)
            {
                var entity = new T();
                //遍历该对象的所有属性  
                foreach (PropertyInfo p in propertys)
                {
                    //将属性名称赋值给临时变量
                    string tmpName = p.Name;
                    //检查DataTable是否包含此列(列名==对象的属性名)    
                    if (!dt.Columns.Contains(tmpName)) continue;
                    //取值  转化格式 
                    object value = Convert.ChangeType(dr[tmpName], p.PropertyType);
                    //如果非空,则赋给对象的属性  
                    if (value != DBNull.Value)
                    {
                        
                        p.SetValue(entity, value, null);
                    }
                }
                //对象添加到泛型集合中  
                list.Add(entity);
            }
            return list;
        }
    }
发布了22 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Street_Walker/article/details/92845180