当前位置:
首页 >
C#_List转换成DataTable
发布时间:2025/4/9
40
豆豆
生活随笔
收集整理的这篇文章主要介绍了
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性能优化-慢查询分析、优化索引
- 下一篇: Redis总结(二)C#中如何使用red