using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public static void Main()
DataTable table = new DataTable();
table.Columns.Add("Product P/N", typeof(string));
table.Columns.Add("Brand", typeof(string));
table.Rows.Add("12", "A");
table.Rows.Add("34", "B");
var list = table.ToList<TableData>();
var a = list.FirstOrDefault().ProductPN;
var b = list.FirstOrDefault().Brand;
foreach (var item in list)
Console.WriteLine(item.ToString());
Console.WriteLine("---");
public static class Extensions
public static List<T> ToList<T>(this DataTable table)
IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
IList<T> result = new List<T>();
foreach (var row in table.Rows)
var item = MappingItem<T>((DataRow)row, properties);
private static T MappingItem<T>(DataRow row, IList<PropertyInfo> properties)
foreach (var property in properties)
var PtName = property.Name;
if (!string.IsNullOrEmpty(property.ToDescription()) && row.Table.Columns.Contains(property.ToDescription()))
PtName = property.ToDescription();
if (row.Table.Columns.Contains(PtName))
if (property.PropertyType == typeof(string))
if (row[PtName] != null && row[PtName] != DBNull.Value)
property.SetValue(item, row[PtName].ToString(), null);
property.SetValue(item, string.Empty, null);
else if (property.PropertyType == typeof(bool))
if (row[PtName].ToString().ToUpper() == "TRUE")
property.SetValue(item, true, null);
property.SetValue(item, false, null);
else if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime? ))
DateTime dt = new DateTime();
if (row[PtName] != DBNull.Value && DateTime.TryParse(row[PtName].ToString(), out dt))
property.SetValue(item, dt, null);
property.SetValue(item, null, null);
else if (property.PropertyType == typeof(decimal))
decimal val = new decimal ();
decimal.TryParse(row[PtName].ToString(), out val);
property.SetValue(item, val, null);
else if (property.PropertyType == typeof(decimal? ))
if (row[PtName] == DBNull.Value || row[PtName].ToString() == null)
property.SetValue(item, null, null);
decimal val = new decimal ();
decimal.TryParse(row[PtName].ToString(), out val);
property.SetValue(item, val, null);
else if (property.PropertyType == typeof(double))
decimal val = new decimal ();
decimal.TryParse(row[PtName].ToString(), out val);
property.SetValue(item, Convert.ToDouble(val), null);
else if (property.PropertyType == typeof(Int16))
decimal val = new decimal ();
decimal.TryParse(row[PtName].ToString(), out val);
property.SetValue(item, Convert.ToInt16(val), null);
else if (property.PropertyType == typeof(Int32))
decimal val = new decimal ();
decimal.TryParse(row[PtName].ToString(), out val);
property.SetValue(item, Convert.ToInt32(val), null);
else if (property.PropertyType == typeof(Int64))
decimal val = new decimal ();
decimal.TryParse(row[PtName].ToString(), out val);
property.SetValue(item, Convert.ToInt64(val), null);
if (row[PtName] != DBNull.Value)
property.SetValue(item, row[PtName], null);
public static string ToDescription(this PropertyInfo property)
object[] descriptionAttrs = property.GetCustomAttributes(typeof(JsonPropertyAttribute), false);
if (descriptionAttrs != null && descriptionAttrs.Length > 0)
JsonPropertyAttribute description = (JsonPropertyAttribute)descriptionAttrs[0];
return description.PropertyName;
object[] attrs = property.GetCustomAttributes(typeof(DisplayAttribute), false);
if (null != attrs && attrs.Length > 0)
return ((DisplayAttribute)attrs[0]).Name;
return property.Name.ToString();
[JsonProperty(PropertyName = "Product P/N")]
public override string ToString()
return String.Format("Product P/N: {0}, Brand: {1}", ProductPN, Brand);