首页 > 代码库 > list<T>转换为datatable

list<T>转换为datatable

 

 

        /// <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