CSharp examples for System.Collections.Generic:List
Convert DataTable To List
using System.Reflection; using System.Collections; using System.ComponentModel; using System.Data; using System.Text; using System.Linq; using System.Collections.Generic; using System;//from ww w .j a v a 2 s .c o m public class Main{ public static List<T> ConvertDataTableToList<T>(DataTable dt) { List<T> lst = new System.Collections.Generic.List<T>(); Type tClass = typeof(T); PropertyInfo[] pClass = tClass.GetProperties(); List<DataColumn> dc = new List<DataColumn>(); dc = dt.Columns.Cast<DataColumn>().ToList(); T cn; foreach (DataRow item in dt.Rows) { cn = (T)Activator.CreateInstance(tClass); foreach (PropertyInfo pc in pClass) { // Can comment try catch block. try { DataColumn d = dc.Find(c => c.ColumnName == pc.Name); if (d != null) pc.SetValue(cn, item[pc.Name], null); } catch { } } lst.Add(cn); } return lst; } }