C#将List集合类转换成DataTable
2021/9/15 17:34:52
本文主要是介绍C#将List集合类转换成DataTable,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Reflection; namespace LifeDecidesHappiness.Net.Utility.ListDataTable { /// <summary> /// List集合 转 DataTable 帮助类 /// https://www.cnblogs.com/LifeDecidesHappiness/p/15273203.html /// LDH @ 2021-9-15 /// </summary> public class List2DataTableHelper { /// <summary> /// 将集合转为DataTable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static DataTable ToDataTable<T>(IEnumerable<T> list) { // 创建属性的集合 var pList = new List<PropertyInfo>(); // 获得反射的入口 var type = typeof(T); var dt = new DataTable(); // 把所有的public属性加入到集合 并添加DataTable的列 Array.ForEach(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); }); foreach (var item in list) { // 创建一个DataRow实例 var row = dt.NewRow(); // 给row 赋值 pList.ForEach(p => row[p.Name] = p.GetValue(item, null)); // 加入到DataTable dt.Rows.Add(row); } return dt; } /// <summary> /// 将List集合类转换成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable List2DataTable(IList list) { var result = new DataTable(); if (list.Count > 0) { var properties = list[0].GetType().GetProperties(); foreach (var pi in properties) result.Columns.Add(pi.Name, pi.PropertyType); foreach (var t in list) { var tempList = new ArrayList(); foreach (var pi in properties) { var obj = pi.GetValue(t, null); tempList.Add(obj); } var array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } #region Convert a List{T} to a DataTable. /// <summary> /// Convert a List{T} to a DataTable. /// </summary> public static DataTable ToDataTable<T>(List<T> items) { var tb = new DataTable(typeof(T).Name); var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in props) { var t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (var item in items) { var values = new object[props.Length]; for (var i = 0; i < props.Length; i++) values[i] = props[i].GetValue(item, null); tb.Rows.Add(values); } return tb; } /// <summary> /// Determine of specified type is nullable /// </summary> public static bool IsNullable(Type t) { return !t.IsValueType || t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>); } /// <summary> /// Return underlying type if type is Nullable otherwise return the type /// </summary> public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!t.IsValueType) return t; return Nullable.GetUnderlyingType(t); } return t; } #endregion } }
这篇关于C#将List集合类转换成DataTable的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID