將DataTable轉(zhuǎn)換成List<T>實(shí)現(xiàn)思路及示例代碼
前幾天在工作中,遇到一個(gè)問(wèn)題:需要將查詢(xún)出來(lái)的DataTable數(shù)據(jù)源,轉(zhuǎn)換成List<T>的泛型集合(已知T類(lèi)型)。第一反應(yīng),我想肯定要用到“泛型”(這不是廢話(huà)嗎?都說(shuō)了要轉(zhuǎn)換成List<T>泛型集合了),而且還要用到“反射”相關(guān)的。呵呵。很快,我就做出了一個(gè)小實(shí)例,測(cè)試通過(guò)。下面我將代碼貼出來(lái),分享給大家。代碼都有詳細(xì)的注釋?zhuān)x者朋友可以很清晰的看懂我的思路。
首先,這是我寫(xiě)的一個(gè)通用轉(zhuǎn)換類(lèi),完成此類(lèi)操作。也是實(shí)現(xiàn)這個(gè)功能最核心的部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Reflection;
namespace DatableToList
{
class ConvertHelper<T> where T : new()
{
/// <summary>
/// 利用反射和泛型
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToList(DataTable dt)
{
// 定義集合
List<T> ts = new List<T>();
// 獲得此模型的類(lèi)型
Type type = typeof(T);
//定義一個(gè)臨時(shí)變量
string tempName = string.Empty;
//遍歷DataTable中所有的數(shù)據(jù)行
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 獲得此模型的公共屬性
PropertyInfo[] propertys = t.GetType().GetProperties();
//遍歷該對(duì)象的所有屬性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;//將屬性名稱(chēng)賦值給臨時(shí)變量
//檢查DataTable是否包含此列(列名==對(duì)象的屬性名)
if (dt.Columns.Contains(tempName))
{
// 判斷此屬性是否有Setter
if (!pi.CanWrite) continue;//該屬性不可寫(xiě),直接跳出
//取值
object value = dr[tempName];
//如果非空,則賦給對(duì)象的屬性
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
//對(duì)象添加到泛型集合中
ts.Add(t);
}
return ts;
}
}
}
下面,是Main方法中調(diào)用的實(shí)例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Reflection;
namespace DatableToList
{
class Program
{
static void Main(string[] args)
{
DataTable dt = CreateDt();//獲得一個(gè)DataTable
//根據(jù)對(duì)象類(lèi)型和DataTable,獲取泛型集合
List<Person> list = ConvertHelper<Person>.ConvertToList(dt);
//遍歷該泛型集合,打印輸出。
foreach (var item in list)
{
Console.WriteLine(item.ToString());
}
Console.ReadKey();
}
/// <summary>
/// 創(chuàng)建一個(gè)DataTable,并添加數(shù)據(jù),提供測(cè)試。
/// </summary>
/// <returns></returns>
public static DataTable CreateDt()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("name", typeof(System.String)));
dt.Columns.Add(new DataColumn("address", typeof(System.String)));
dt.Rows.Add(1,"Dylan","SZ");
dt.Rows.Add(2, "Jay", "TW");
dt.Rows.Add(3, "CQ", "HK");
return dt;
}
}
}
最下面,就是自定義的一個(gè)簡(jiǎn)單類(lèi)的代碼了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DatableToList
{
class Person
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
public override string ToString()
{
return "Person: " + id + " ," + name+","+address;
}
}
}
上面的例子測(cè)試通過(guò),水平有限,不足之處,敬請(qǐng)諒解。
相關(guān)文章
C#使用SQL Dataset數(shù)據(jù)集代碼實(shí)例
今天小編就為大家分享一篇關(guān)于的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10C#調(diào)用WebService實(shí)例開(kāi)發(fā)
那么,我們?cè)趺丛陧?xiàng)目中調(diào)用WebService這個(gè)方法呢,其實(shí)這和調(diào)用天氣的webservice是一個(gè)道理,首先,通過(guò)添加“web服務(wù) 引用”將,你寫(xiě)的webservice引用進(jìn)來(lái),我們需要注意的是其中有一處要我們填寫(xiě)請(qǐng)求webservice的URL地址,我們?cè)撛趺磳?xiě)?2015-09-09C#實(shí)現(xiàn)的XML操作類(lèi)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的XML操作類(lèi),涉及C#操作XML文件的讀取、插入、修改、刪除等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#?基于TCP?實(shí)現(xiàn)掃描指定ip端口的方式示例
本文主要介紹了C#基于TCP實(shí)現(xiàn)掃描指定ip端口的方式示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01詳解C#中多態(tài)性學(xué)習(xí)/虛方法/抽象方法和接口的用法
這篇文章主要為大家詳細(xì)介紹了C#中多態(tài)性學(xué)習(xí)、虛方法、抽象方法和接口的用法的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03winform 調(diào)用攝像頭掃碼識(shí)別二維碼的實(shí)現(xiàn)步驟
這篇文章主要介紹了winform 調(diào)用攝像頭掃碼識(shí)別二維碼的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用winform,感興趣的朋友可以了解下2021-02-02