资讯动态

C# IEnumerable<T>转换成DataTable

发布时间:2026/9/2 12:32:37 来源:尧图企业网站定制
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;}

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价