首页 > 其他 > 详细

list<T>转换为datatable

时间:2016-10-08 13:30:40      阅读:108      评论:0      收藏:0      [点我收藏+]

 

 

        /// <summary>
        /// 将list<t>转换为datatable
        /// </summary>
        /// <param name="list">需要转换的数据</param>
        /// <returns></returns>
        private DataTable getDT(List<SearchRecodVM> list)
        {
            AddMemberLog(VarKey.Page.SearchRecord.ToString(), VarKey.OperateType.Query, BaseRes.LOG_MSG_001 + LoginUser.MemberName + BaseRes.LOG_MSG_031);
            //创建属性的集合  
            List<PropertyInfo> pList = new List<PropertyInfo>();
            //获得反射的入口  
            Type type = typeof(SearchRecodVM);
            DataTable dt = new DataTable();
            //把所有的public属性加入到集合 并添加DataTable的列  
            Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
            foreach (var item in list)
            {
                //创建一个DataRow实例  
                DataRow row = dt.NewRow();
                //给row 赋值  
                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                //加入到DataTable  
                dt.Rows.Add(row);
            }

            return dt
        }

  

  

  

list<T>转换为datatable

原文:http://www.cnblogs.com/lhlong/p/5937999.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!