您的位置:首页 > 其它

DataTable转换成List<T>

2012-02-15 14:38 579 查看
List<Person> persons = new List<Person>();

int rows = dt.Rows.Count;

if (rows > 0)

{

Person person;

for (int i = 0; i < rows; i++)

{

person = new Person();

if (dt.Rows[i]["ID"] != null && dt.Rows[i]["ID"].ToString() != "")

{ person.ID = int.Parse(dt.Rows[i]["ID"].ToString()); }

if (dt.Rows[i]["Age"] != null && dt.Rows[i]["Age"].ToString() != "")

{ person.Age = dt.Rows[i]["Age"].ToString(); }

if (dt.Rows[i]["Lover"] != null && dt.Rows[i]["Lover"].ToString() != "")

{ person.Lover =dt.Rows[i]["Lover"].ToString(); }

if (dt.Rows[i]["Name"] != null && dt.Rows[i]["Name"].ToString() != "")

{ person.Name = dt.Rows[i]["Name"].ToString(); }

persons.Add(person);

}

}

List<Person> per = persons.FindAll(p=>p.Name=="Name1");

this.Label1.Text = per[0].Name;

、、、、、、、、、、、、原文、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

Person类:

public class Person

{

public Person()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

public int ID { set; get; }

public string Name { set; get; }

public string Age { set; get; }

public string Lover { set; get; }

}

IBToListTest.cs:

public class IBToListTest<T> where T :new()

{

public IBToListTest()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

private IList<string> GetColumnNames(DataColumnCollection dcc)

{

IList<string> list = new List<string>();

foreach (DataColumn dc in dcc)

{

list.Add(dc.ColumnName);

}

return list;

}

private Hashtable GetColumnType(DataColumnCollection dcc)

{

if (dcc == null || dcc.Count == 0)

{

return null;

}

IList<string> colNameList = GetColumnNames(dcc);

Type t=typeof(T);

PropertyInfo[] properties = t.GetProperties();

Hashtable hashtable = new Hashtable();

int i = 0;

foreach (PropertyInfo p in properties)

{

foreach (string col in colNameList)

{

if (col.ToLower().Contains(p.Name.ToLower()))

{

hashtable.Add(col ,p.PropertyType.ToString()+i++);

}

}

}

return hashtable;

}

private IList<string> GetColumnNames(Hashtable hh)

{

PropertyInfo[] properties = typeof(T).GetProperties();

IList<string> ilist = new List<string>();

int i = 0;

foreach (PropertyInfo p in properties)

{

ilist.Add(GetKey(p.PropertyType.ToString()+i++,hh));

}

return ilist;

}

private string GetKey(string val, Hashtable hh)

{

foreach (DictionaryEntry de in hh)

{

if (de.Value.ToString() == val)

{

return de.Key.ToString();

}

}

return null;

}

public IList<T> ToList(DataTable dt)

{

if (dt == null || dt.Rows.Count == 0)

{

return null;

}

PropertyInfo[] properties = typeof(T).GetProperties();

Hashtable hh = GetColumnType(dt.Columns);

IList<string> colNames = GetColumnNames(hh);

List<T> list = new List<T>();

T model=default(T);

foreach (DataRow dr in dt.Rows)

{

model = new T();

int i = 0;

foreach (PropertyInfo p in properties)

{

if (p.PropertyType == typeof(string))

{

p.SetValue(model, dr[colNames[i++]], null);

}

else if (p.PropertyType == typeof(int))

{

p.SetValue(model, int.Parse(dr[colNames[i++]].ToString()), null);

}

else if (p.PropertyType == typeof(DateTime))

{

p.SetValue(model, DateTime.Parse(dr[colNames[i++]].ToString()), null);

}

else if (p.PropertyType == typeof(float))

{

p.SetValue(model, float.Parse(dr[colNames[i++]].ToString()), null);

}

else if (p.PropertyType == typeof(double))

{

p.SetValue(model, double.Parse(dr[colNames[i++]].ToString()), null);

}

}

list.Add(model);

}

return list;

}

}

页面后台:

IBToListTest<Person> tol = new IBToListTest<Person>();

protected void Page_Load(object sender, EventArgs e)

{

DataTable dt = GetTable();

tol.ToList(dt);

IList<Person> ps = tol.ToList(dt);

foreach (Person p in ps)

{

Response.Write(p.Name);

}

}

public static DataTable GetTable()

{

DataTable dt = new DataTable();

dt.Columns.Add("ID");

dt.Columns.Add("Age");

dt.Columns.Add("Lover");

dt.Columns.Add("Name");

DataRow dr = dt.NewRow();

dr["ID"] = 1;

dr["Age"] = "Age1";

dr["Lover"] = "Lover1";

dr["Name"] = "Name1";

dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();

dr1["ID"] = 2;

dr1["Age"] = "Age2";

dr1["Lover"] = "Lover2";

dr1["Name"] = "Name2";

dt.Rows.Add(dr1);

return dt;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: