C# 把List 集合转换成DataTable
程序员文章站
2022-05-01 19:46:41
/// /// 将List集合 转换成 DataTable /// /// sellerSearchDealList是我自己定义的一个类 public static DataTable ListToTable(List
/// <summary>
/// 将List集合 转换成 DataTable
/// </summary>
/// <param name="entitys">sellerSearchDealList是我自己定义的一个类</param>
public static DataTable ListToTable(List<object> entitys)
{
DataTable dtresult = new DataTable();
if (entitys == null || entitys.Count < 1)
{
throw new Exception("空");
}
else
{
PropertyInfo[] propertys = entitys[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
dtresult.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < entitys.Count; i++)
{
ArrayList tenpList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(entitys[i], null);
tenpList.Add(obj);
}
object[] array = tenpList.ToArray();
dtresult.LoadDataRow(array, true);
}
}
return dtresult;
}
本文地址:https://blog.csdn.net/weixin_40362806/article/details/108992669
上一篇: 不认识
推荐阅读
-
C#中的DataSet、string、DataTable、对象转换成Json的实现代码
-
c#的datatable转list示例
-
C#把数字转换成大写金额的代码实例
-
C#中把Datatable转换为Json的5个代码实例
-
C#中的DataSet、string、DataTable、对象转换成Json的实现代码
-
C#将DataTable转换成list的方法
-
C#将DataTable转换成list的方法
-
C#实现把图片转换成二进制以及把二进制转换成图片的方法示例
-
C#实现把图片转换成二进制以及把二进制转换成图片的方法示例
-
【转载】C#中List集合使用RemoveRange方法移除指定索引开始的一段元素