链接:http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html#2738813 留着学习
using System; using System.Collections.Generic; using System.Collections; using System.Data; namespace CTHFramework { /// <summary> /// DataTable转换成IList /// </summary> /// <typeparam name="T"></typeparam> public class TableToList<T> where T : class, new() { public static IList<T> ToList(DataTable dt, Func<DataRow, T> func) { if (dt == null || dt.Rows.Count == 0) { return null; } IList<T> list = new List<T>(dt.Rows.Count); foreach (DataRow dr in dt.Rows) { list.Add(func(dr)); } return list; } } public class DataItem { public int ID { get; set; } public string Name { get; set; } public override string ToString() { return string.Format("ID:{0},Name:{1}", ID.ToString(), Name); } } static class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ID", typeof(int))); dt.Columns.Add(new DataColumn("Name", typeof(string))); for (int i = 0; i < 10; i++) { DataRow dr = dt.NewRow(); dr["ID"] = i.ToString(); dr["Name"] = "Name" + i.ToString(); dt.Rows.Add(dr); } IList<DataItem> list = TableToList<DataItem>.ToList(dt, DataRowToDataItem); foreach (var item in list) { Console.WriteLine(item.ToString()); } Console.Read(); } static DataItem DataRowToDataItem(DataRow dr) { return new DataItem { ID = Convert.ToInt32(dr["ID"]), Name = dr["Name"].ToString() }; } } }