c#將list類型轉(zhuǎn)換成DataTable方法示例
更新時間:2013年12月26日 09:51:36 作者:
將List類型轉(zhuǎn)換成DataTable的通用方法,大家參考使用吧
復(fù)制代碼 代碼如下:
/// <summary>
/// 將List轉(zhuǎn)換成DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable dt = new DataTable();
for (int i = 0; i < properties.Count; i++)
{
PropertyDescriptor property = properties[i];
dt.Columns.Add(property.Name, property.PropertyType);
}
object[] values = new object[properties.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = properties[i].GetValue(item);
}
dt.Rows.Add(values);
}
return dt;
}
您可能感興趣的文章:
相關(guān)文章
C# List 并發(fā)丟數(shù)據(jù)問題原因及解決方案
這篇文章主要介紹了C# List 并發(fā)丟數(shù)據(jù)問題原因及解決方案,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn)
本文主要介紹了C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07