转换匿名类型数据表数据表、类型

由网友(沐风)分享简介:什么是匿名类型转换为数据表最快的方法是什么? What is the fastest way to convert anonymous type to DataTable? 更新:我想和匿名类型填充数据表。如果反射neccesary,我怎么能使用反射办呢?Update:I want to get and pop...

什么是匿名类型转换为数据表最快的方法是什么?

What is the fastest way to convert anonymous type to DataTable?

更新: 我想和匿名类型填充数据表。如果反射neccesary,我怎么能使用反射办呢?

Update: I want to get and populate DataTable from anonymous type. If reflection is neccesary, how can I to do it using reflection?

推荐答案

在这里找到:http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/6d4e2579-f4b5-497c-ad00-7c347eb1f902/

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: Generic List to DataTable

阅读全文

相关推荐

最新文章