public static DataTable ListToDataTable<T>(List<T> list)
{
List<PropertyInfo> pList = new List<PropertyInfo>();
DataTable dt = new DataTable();
Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p =>
{
if (p.CustomAttributes.Count() == 0||(p.CustomAttributes.Count()>0&&p.Name=="PK_ID"))//如果>0代表是有特性入主键标识或扩展标识的,如果是扩展标识就不添加列这样到时候用sqlbulkcopy插入数据库才不会出问题
{
pList.Add(p);
dt.Columns.Add(p.Name, p.PropertyType);
}
});
list.ForEach(item =>
{
DataRow row = dt.NewRow();
foreach(PropertyInfo info in pList)
{
12-06
2082
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交