c# DataTable to Object Mapping

public static class DataTableExtensions
{
   public static IList<T> ToList<T>(this DataTable table) where T : new()
   {
      IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
      IList<T> result = new List<T>();

      foreach (var row in table.Rows)
      {
         var item = CreateItemFromRow<T>((DataRow)row, properties);
         result.Add(item);
      }

      return result;
   }

   private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
   {
       T item = new T();
       foreach (var property in properties)
       {
           property.SetValue(item, row[property.Name], null);
       }
       return item;
   }
}

 

posted @ 2017-09-28 17:35  akiing  阅读(775)  评论(0编辑  收藏  举报