转换匿名类型数据表 [英] Converting anonymous type to DataTable

查看:110
本文介绍了转换匿名类型数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

在这里找到:<一href="http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/6d4e2579-f4b5-497c-ad00-7c347eb1f902/">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

这篇关于转换匿名类型数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆