在我下面的函数中...我想返回 DataTable ..
我应该转换以下行
DataTable table = CreateTable<T>();
跟随
using(DataTable table = CreateTable<T>())
{
}
功能
public static DataTable ConvertTo<T>(IList<T> list)
{
DataTable table = CreateTable<T>();
int iColCount = table.Columns.Count;
for (int j = 0; j < iColCount; j++)
{
DataColumn myDC = table.Columns[j];
myDC.DataType = System.Type.GetType("System.String");
}
Type entityType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
} table.Rows.Add(row);
}
return table;
}