ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

C# IEnumerable<T>转换成DataTable

C# IEnumerable<T>转换成DataTable 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;}
返回列表