码迷,mamicode.com
首页 > 其他好文 > 详细

任意List 和DatabTable的转换

时间:2016-11-19 12:01:10      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:public   datarow   datatable   tab   count   cti   turn   val   amp   

 public static IEnumerable<T> ToEntityList<T>(this DataTable table) where T : class
        {
            var entityList = new List<T>();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow dr in table.Rows)
                {
                    var entity = (T)Activator.CreateInstance(typeof(T));
                    for (int i = 0; i < dr.Table.Columns.Count; i++)
                    {
                        PropertyInfo propertyInfo = entity.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                        if (propertyInfo != null && dr[i] != DBNull.Value)
                        {
                            propertyInfo.SetValue(entity, dr[i], null);
                        }
                    }
                    entityList.Add(entity);
                }
            }
            return entityList;
        }

任意List 和DatabTable的转换

标签:public   datarow   datatable   tab   count   cti   turn   val   amp   

原文地址:http://www.cnblogs.com/damsoft/p/6079993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!