恒美微站
首页
关于我们
建站服务
主题模板
案例展示
资讯中心
联系我们
C# IEnumerable<T>转换成DataTable
首页
资讯中心
/
C# IEnumerable<T>转换成DataTable
C# IEnumerable<T>转换成DataTable
发布时间:2026/9/2 12:33:08
publicstaticDataTableToDataTableT(thisIEnumerableTlist){DataTabledataTablenewDataTable();PropertyInfo[]arraynull;foreach(Titeminlist){if(arraynull){arrayitem.GetType().GetProperties();PropertyInfo[]array2array;foreach(PropertyInfopropertyInfoinarray2){TypetypepropertyInfo.PropertyType;if(type.IsGenericTypetype.GetGenericTypeDefinition()typeof(Nullable)){typetype.GetGenericArguments()[0];}dataTable.Columns.Add(newDataColumn(propertyInfo.Name,type));}}DataRowdataRowdataTable.NewRow();PropertyInfo[]array3array;foreach(PropertyInfopropertyInfo2inarray3){dataRow[propertyInfo2.Name]((propertyInfo2.GetValue(item,null)null)?DBNull.Value:propertyInfo2.GetValue(item,null));}dataTable.Rows.Add(dataRow);}returndataTable;}