Article Source:
Http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html
DataTable converted to IList
When using C # for development, it is often useful to convert a DataTable to IList, to manipulate a DataTable to be cumbersome, to convert a DataTable to IList, and to object entities as an element of IList.
Note: The attribute of an entity must correspond to a field in the database, one by one, or a database field name. ToLower (). Contains (Entity property name. ToLower ())
Data type temporarily to support int, string, DateTime, float, double
Using System;
Using System.Collections;
Using System.Collections.Generic;
Using System.Data;
Using System.Reflection;
Namespace Tbtolisttest
{
public class tbtolist<t> where T:new ()
{
<summary>
Get Column Name Collection
</summary>
Private ilist<string> Getcolumnnames (datacolumncollection DCC)
{
ilist<string> list = new list<string> ();
foreach (DataColumn dc in DCC)
{
List. ADD (DC. ColumnName);
}
return list;
}
<summary>
Collection of key-value pairs for property names and type names
</summary>
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;
}
<summary>
DataTable converted to IList
</summary>
<param name= "DT" ></param>
<returns></returns>
Public ilist<t> ToList (DataTable DT)
{
if (dt = = NULL | | dt. Rows.Count = = 0)
{
return null;
}
Propertyinfo[] Properties = typeof (T). GetProperties ();//Gets the collection of attributes for an entity type
Hashtable hh = getcolumntype (dt. Columns);//The collection of key-value pairs for the property name and type name
Ilist<string> colnames = Getcolumnnames (hh);//collection of column names by attribute order
list<t> list = new list<t> ();
T model = default (t);
foreach (DataRow dr in Dt. Rows)
{
Model = new T ();//Create Entity
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;
}
<summary>
Collection of column names by attribute order
</summary>
Private ilist<string> getcolumnnames (Hashtable hh)
{
Propertyinfo[] Properties = typeof (T). GetProperties ();//Gets the collection of attributes for an entity type
ilist<string> ilist = new list<string> ();
int i = 0;
foreach (PropertyInfo p in properties)
{
IList. ADD (GetKey (p.propertytype.tostring () + i++, hh));
}
return IList;
}
<summary>
Find key based on value
</summary>
private String GetKey (string val, Hashtable TB)
{
foreach (DictionaryEntry de in TB)
{
if (DE. value.tostring () = = val)
{
Return de. Key.tostring ();
}
}
return null;
}
}
}
Namespace Tbtolisttest
{
Entity
public class Person
{
public int ID
{
Set
Get
}
public string Name
{
Set
Get
}
public string Age
{
Set
Get
}
public string Lover
{
Set
Get
}
}
}
Using System;
Using System.Data;
Namespace Tbtolisttest
{
Class Program
{
static void Main (string[] args)
{
tbtolist<person> tol = new tbtolist<person> ();
Console.WriteLine ();
DataTable dt = GetTable ();
Tol. ToList (DT);
Console.read ();
}
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;
}
}
}
DataTable converted to IList