What is the fastest way to convert anonymous type to DataTable?
Update: I want to get and populate DataTable from anonymous type. If reflection is neccesary, how can I to do it using reflection?
Found here:
var result = from p in dataSource 
             group p by p.City into cities 
             select new { Property1 = cities.Key, Property 2= cities.Average(p => p.Age) }; 
dt.Columns.Add("Property1"); 
dt.Columns.Add("Property2"); 
foreach (var item in result) 
{   
    dt.Rows.Add(item.Property1,item.Property2);                 
}
See here for a generic solution: Convert generic List/Enumerable to DataTable?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With