document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //List to DataTable
  2.  
  3. //Here is the extension method to convert a List to a DataTable:
  4.  
  5. /// <summary>
  6. /// Convert Genric List  to DataTable
  7. /// </summary>
  8. public static DataTable ToDataTable<T>(this IList<T> listObject)
  9. {
  10.     PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
  11.     DataTable myTable = new DataTable();
  12.     foreach (PropertyDescriptor prop in properties)
  13.     {
  14.         myTable.Columns.Add(prop.Name, prop.PropertyType);
  15.     }
  16.     object[] values = new object[properties.Count];
  17.     foreach (T item in listObject)
  18.     {
  19.         for (int i = 0; i < values.Length; i++)
  20.         {
  21.             values[i] = properties[i].GetValue(item);
  22.         }
  23.         myTable.Rows.Add(values);
  24.     }
  25.     return myTable;
  26. }
  27.  
  28.  
  29. //DataTable to List<TSource>
  30.  
  31. /// <summary>
  32. /// Convert DataTable to Genric List
  33. /// </summary>
  34. public static List<T> DataTableMapToList<T>(DataTable dtSource)
  35. {
  36.     string propName = string.Empty;
  37.     List<T> entityList = new List<T>();
  38.     foreach (DataRow dr in dtSource.Rows)
  39.     {
  40.         // Create Instance of the Type T
  41.         T entity = Activator.CreateInstance<T>();
  42.         // Get all properties of the Type T
  43.         System.Reflection.PropertyInfo[] entityProperties = typeof(T).GetProperties();
  44.         // Loop through the properties defined in the
  45.         // entityList entity object and mapped the value
  46.         foreach (System.Reflection.PropertyInfo item in entityProperties)
  47.         {
  48.             propName = string.Empty;
  49.             if (propName.Equals(string.Empty))
  50.                 propName = item.Name;
  51.             if (dtSource.Columns.Contains(propName))
  52.             {
  53.                 // Assign value to the property
  54.                 item.SetValue
  55.                 (
  56.                     entity,
  57.                     dr[propName].GetType().Name.Equals(typeof(DBNull).Name) ? null : dr[propName],
  58.                     null
  59.                 );
  60.             }
  61.         }
  62.         entityList.Add(entity);
  63.     }
  64.     return entityList;
  65. }
  66.  
  67.  
  68. //====================================================================================================================
  69.  
  70.  
  71. /*Converts List To DataTable*/
  72. public static DataTable ToDataTable<TSource>(this IList<TSource> data)
  73. {
  74.     DataTable dataTable = new DataTable(typeof(TSource).Name);
  75.     PropertyInfo[] props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  76.     foreach (PropertyInfo prop in props)
  77.     {
  78.         dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
  79.     }
  80.  
  81.     foreach (TSource item in data)
  82.     {
  83.         var values = new object[props.Length];
  84.         for (int i = 0; i < props.Length; i++)
  85.         {
  86.             values[i] = props[i].GetValue(item, null);
  87.         }
  88.         dataTable.Rows.Add(values);
  89.     }
  90.     return dataTable;
  91. }
  92.  
  93.  
  94. /*Converts DataTable To List*/
  95. public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
  96. {
  97.     var dataList = new List<TSource>();
  98.  
  99.     const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
  100.     var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
  101.                             select new { Name = aProp.Name,
  102.                             Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType }).ToList();
  103.     var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
  104.                                 select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList();
  105.     var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();
  106.    
  107.     foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
  108.     {
  109.         var aTSource = new TSource();
  110.         foreach (var aField in commonFields)
  111.         {
  112.             PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
  113.             propertyInfos.SetValue(aTSource, dataRow[aField.Name], null);
  114.         }
  115.         dataList.Add(aTSource);
  116.     }
  117.     return dataList;
  118. }
');