c#將list類(lèi)型轉(zhuǎn)換成DataTable方法示例
更新時(shí)間:2013年12月26日 09:51:36 作者:
將List類(lèi)型轉(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;
}
您可能感興趣的文章:
- C#中泛型舉例List<T>與DataTable相互轉(zhuǎn)換
- c#中DataTable轉(zhuǎn)List的2種方法示例
- C#中將DataTable轉(zhuǎn)化成List<T>的方法解析
- C#實(shí)現(xiàn)DataTable,List和Json轉(zhuǎn)換的方法
- C#實(shí)現(xiàn)DataTable轉(zhuǎn)換成IList的方法
- C#將DataTable轉(zhuǎn)化為L(zhǎng)ist<T>
- C#將DataTable轉(zhuǎn)換成list的方法
- c#的datatable轉(zhuǎn)list示例
- C#中DataTable和List互轉(zhuǎn)的示例代碼
相關(guān)文章
C# List 并發(fā)丟數(shù)據(jù)問(wèn)題原因及解決方案
這篇文章主要介紹了C# List 并發(fā)丟數(shù)據(jù)問(wèn)題原因及解決方案,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)
本文主要介紹了C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07