C#_List转换成DataTable
生活随笔
收集整理的這篇文章主要介紹了
C#_List转换成DataTable
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/// <summary>/// 講list集合轉換成datatable/// </summary>/// <param name="list"></param>/// <returns></returns>public static System.Data.DataTable ListToDataTable(IList list){System.Data.DataTable result = new System.Data.DataTable();if (list.Count > 0){PropertyInfo[] propertys = list[0].GetType().GetProperties();foreach (PropertyInfo pi in propertys){//獲取類型Type colType = pi.PropertyType;//當類型為Nullable<>時if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))){colType = colType.GetGenericArguments()[0];}result.Columns.Add(pi.Name, colType);}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;}
?
轉載于:https://www.cnblogs.com/ingstyle/p/4166392.html
總結
以上是生活随笔為你收集整理的C#_List转换成DataTable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql性能优化-慢查询分析、优化索引
- 下一篇: 电磁学讲义2:库仑定律