首页 > 其他 > 详细

将集合类转换成DataTable

时间:2014-11-25 10:41:04      阅读:247      评论:0      收藏:0      [点我收藏+]

 /// <summary>        

/// 将集合类转换成DataTale        

/// </summary>        

/// <param name="list">集合</param>        

/// <returns></returns>        

public static DataTable ToDataTable(IList list)         {            

DataTable result = new DataTable();            

if (list == null)            

{                

return result;            

}

 if (list.Count > 0)            

{                

PropertyInfo[] propertys = list[0].GetType().GetProperties();                

foreach (PropertyInfo pi in propertys)                

{                    

result.Columns.Add(pi.Name, pi.PropertyType);                

}

 for (int i = 0; i < list.Count; i++)                

{                    

ArrayList tempList = new ArrayList();                    

foreach (PropertyInfo pi in propertys)                    

{                        

object obj = pi.GetValue(list[i], null);                        

tempList.Add(obj);                    

}                    

object[] array = tempList.ToArray();                    

result.LoadDataRow(array, true);                

}            

}            

return result;        

}

将集合类转换成DataTable

原文:http://www.cnblogs.com/rukialu/p/4120299.html

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