Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might benefit from the helper class in this post: <a href="http://lozanotek.com/blog/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx" rel="nofollow">http://lozanotek.com/blog/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx</a></p> <pre><code>public class CollectionHelper { private CollectionHelper() { } public static DataTable ConvertTo&lt;T&gt;(IList&lt;T&gt; list) { DataTable table = CreateTable&lt;T&gt;(); Type entityType = typeof(T); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); foreach (T item in list) { DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) { row[prop.Name] = prop.GetValue(item); } table.Rows.Add(row); } return table; } public static IList&lt;T&gt; ConvertTo&lt;T&gt;(IList&lt;DataRow&gt; rows) { IList&lt;T&gt; list = null; if (rows != null) { list = new List&lt;T&gt;(); foreach (DataRow row in rows) { T item = CreateItem&lt;T&gt;(row); list.Add(item); } } return list; } public static IList&lt;T&gt; ConvertTo&lt;T&gt;(DataTable table) { if (table == null) { return null; } List&lt;DataRow&gt; rows = new List&lt;DataRow&gt;(); foreach (DataRow row in table.Rows) { rows.Add(row); } return ConvertTo&lt;T&gt;(rows); } public static T CreateItem&lt;T&gt;(DataRow row) { T obj = default(T); if (row != null) { obj = Activator.CreateInstance&lt;T&gt;(); foreach (DataColumn column in row.Table.Columns) { PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName); try { object value = row[column.ColumnName]; prop.SetValue(obj, value, null); } catch { // You can log something here throw; } } } return obj; } public static DataTable CreateTable&lt;T&gt;() { Type entityType = typeof(T); DataTable table = new DataTable(entityType.Name); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); foreach (PropertyDescriptor prop in properties) { table.Columns.Add(prop.Name, prop.PropertyType); } return table; } } To see the full code in action, check this sample out: public class MyClass { public static void Main() { List&lt;Customer&gt; customers = new List&lt;Customer&gt;(); for (int i = 0; i &lt; 10; i++) { Customer c = new Customer(); c.Id = i; c.Name = "Customer " + i.ToString(); customers.Add(c); } DataTable table = CollectionHelper.ConvertTo&lt;Customer&gt;(customers); foreach (DataRow row in table.Rows) { Console.WriteLine("Customer"); Console.WriteLine("---------------"); foreach (DataColumn column in table.Columns) { object value = row[column.ColumnName]; Console.WriteLine("{0}: {1}", column.ColumnName, value); } Console.WriteLine(); } RL(); } #region Helper methods private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload