c# 隐含类型var 转换为 DataTable

/// <summary>

/// 隐含类型var 转换为 DataTable

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="array"></param>

/// <returns></returns>

public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)

{

var ret = new DataTable();

foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))

ret.Columns.Add(dp.Name, dp.PropertyType);

foreach (T item in array)

{

var Row = ret.NewRow();

foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))

Rowdp.Name = dp.GetValue(item);

ret.Rows.Add(Row);

}

return ret;

}

}

相关推荐
小白学大数据30 分钟前
Codex 里的 GPT 6 Astra、GPT 5.6 Sol、Terra、Luna 怎么选
开发语言·gpt·microsoft
l1t2 小时前
DeepSeek 4.1总结的Tom Lane 谈塑造 Postgres 三十年历程的架构决策
开发语言·数据库·postgresql·架构
djarmy8 小时前
MAIN.c(1): warning C318: can’t open file ‘STC8G.H’ 报错 解决 分析
c语言·开发语言·mongodb
HEJOO98 小时前
深入理解 Java volatile 关键字:原理、用法与常见误区
java·开发语言·spring
曹牧8 小时前
Java:no content to map due to end of input
java·开发语言
梦梦代码精8 小时前
《回收租赁系统技术选型避坑指南:业务闭环与二开自由度详解》
开发语言·低代码·docker·开源·代码规范
隔窗听雨眠9 小时前
记一次SQL Server数据库性能分析:从CPU100%到单配置修复的完整诊断
开发语言·数据库·php
David猪大卫9 小时前
【C++修炼】智能指针使用及原理
开发语言·c++·经验分享·笔记·学习·考研·面试
charlie1145141919 小时前
现代Qt开发之图像处理进阶:像素操作与格式转换
开发语言·图像处理·qt
李永奉9 小时前
中科蓝讯SDK开发-BT893x 面条耳机连接vivo手机后连接APP端,最后手机端设置页面手动断开蓝牙连接,此时耳机端假断开
c语言·开发语言·嵌入式硬件·物联网·智能手机·电脑