將DataTable轉(zhuǎn)換成List<T>實(shí)現(xiàn)思路及示例代碼
前幾天在工作中,遇到一個(gè)問題:需要將查詢出來的DataTable數(shù)據(jù)源,轉(zhuǎn)換成List<T>的泛型集合(已知T類型)。第一反應(yīng),我想肯定要用到“泛型”(這不是廢話嗎?都說了要轉(zhuǎn)換成List<T>泛型集合了),而且還要用到“反射”相關(guān)的。呵呵。很快,我就做出了一個(gè)小實(shí)例,測試通過。下面我將代碼貼出來,分享給大家。代碼都有詳細(xì)的注釋,讀者朋友可以很清晰的看懂我的思路。
首先,這是我寫的一個(gè)通用轉(zhuǎn)換類,完成此類操作。也是實(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>();
// 獲得此模型的類型
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;//將屬性名稱賦值給臨時(shí)變量
//檢查DataTable是否包含此列(列名==對(duì)象的屬性名)
if (dt.Columns.Contains(tempName))
{
// 判斷此屬性是否有Setter
if (!pi.CanWrite) continue;//該屬性不可寫,直接跳出
//取值
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ì)象類型和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ù),提供測試。
/// </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è)簡單類的代碼了:
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;
}
}
}
上面的例子測試通過,水平有限,不足之處,敬請諒解。
相關(guān)文章
C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
WinForm項(xiàng)目開發(fā)中Excel用法實(shí)例解析
這篇文章主要介紹了WinForm項(xiàng)目開發(fā)中Excel用法,非常實(shí)用,需要的朋友可以參考下2014-08-08
WindowsForm移動(dòng)一個(gè)沒有標(biāo)題欄的窗口的方法
這篇文章主要介紹了WindowsForm移動(dòng)一個(gè)沒有標(biāo)題欄的窗口的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式
學(xué)完設(shè)計(jì)模式之后,你就感覺它會(huì)慢慢地影響到你寫代碼的思維方式,下面這篇文章主要給大家介紹了關(guān)于C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式的相關(guān)資料,需要的朋友可以參考下2021-08-08
C#中List集合使用Max()方法查找到最大值的實(shí)例
這篇文章主要介紹了C#中List集合使用Max()方法查找到最大值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12

