Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a nice 2013 update using <a href="http://nuget.org/packages/FastMember/" rel="noreferrer">FastMember</a> from NuGet:</p> <pre><code>IEnumerable&lt;SomeType&gt; data = ... DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data)) { table.Load(reader); } </code></pre> <p>This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it to particular members (or enforce the order), then you can do that too:</p> <pre><code>IEnumerable&lt;SomeType&gt; data = ... DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data, "Id", "Name", "Description")) { table.Load(reader); } </code></pre> <p>Editor's <em>Dis</em>/<strong>claimer:</strong> FastMember is a Marc Gravell project. Its gold and full-on flys!</p> <hr> <p>Yes, this is pretty much the exact opposite of <a href="https://stackoverflow.com/questions/545328/asp-net-potential-memory-leaks-converting-datatable-to-lists/545429#545429">this</a> one; reflection would suffice - or if you need quicker, <a href="http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx" rel="noreferrer"><code>HyperDescriptor</code></a> in 2.0, or maybe <code>Expression</code> in 3.5. Actually, <code>HyperDescriptor</code> should be more than adequate.</p> <p>For example:</p> <pre><code>// remove "this" if not on C# 3.0 / .NET 3.5 public static DataTable ToDataTable&lt;T&gt;(this IList&lt;T&gt; data) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); for(int i = 0 ; i &lt; props.Count ; i++) { PropertyDescriptor prop = props[i]; table.Columns.Add(prop.Name, prop.PropertyType); } object[] values = new object[props.Count]; foreach (T item in data) { for (int i = 0; i &lt; values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } return table; } </code></pre> <p>Now with one line you can make this many many times faster than reflection (by enabling <a href="http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx" rel="noreferrer"><code>HyperDescriptor</code></a> for the object-type <code>T</code>).</p> <hr> <p>edit re performance query; here's a test rig with results:</p> <pre><code>Vanilla 27179 Hyper 6997 </code></pre> <p>I suspect that the bottleneck has shifted from member-access to <code>DataTable</code> performance... I doubt you'll improve much on that...</p> <p>code:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; public class MyData { public int A { get; set; } public string B { get; set; } public DateTime C { get; set; } public decimal D { get; set; } public string E { get; set; } public int F { get; set; } } static class Program { static void RunTest(List&lt;MyData&gt; data, string caption) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); GC.WaitForFullGCComplete(); Stopwatch watch = Stopwatch.StartNew(); for (int i = 0; i &lt; 500; i++) { data.ToDataTable(); } watch.Stop(); Console.WriteLine(caption + "\t" + watch.ElapsedMilliseconds); } static void Main() { List&lt;MyData&gt; foos = new List&lt;MyData&gt;(); for (int i = 0 ; i &lt; 5000 ; i++ ){ foos.Add(new MyData { // just gibberish... A = i, B = i.ToString(), C = DateTime.Now.AddSeconds(i), D = i, E = "hello", F = i * 2 }); } RunTest(foos, "Vanilla"); Hyper.ComponentModel.HyperTypeDescriptionProvider.Add( typeof(MyData)); RunTest(foos, "Hyper"); Console.ReadLine(); // return to exit } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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