惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
I
InfoQ
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
博客园_首页
The Cloudflare Blog
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
罗磊的独立博客
月光博客
月光博客
V
V2EX
大猫的无限游戏
大猫的无限游戏
腾讯CDC
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
B
Blog RSS Feed
Webroot Blog
Webroot Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
C
CERT Recently Published Vulnerability Notes
人人都是产品经理
人人都是产品经理
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Announcements
Recent Announcements
Cyberwarzone
Cyberwarzone
G
Google Developers Blog
H
Heimdal Security Blog
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
T
Tenable Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
O
OpenAI News
C
Check Point Blog
Forbes - Security
Forbes - Security
SecWiki News
SecWiki News
K
Kaspersky official blog
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Full Disclosure
阮一峰的网络日志
阮一峰的网络日志

博客园 - WEBBER

SQL语句中使用变量作为条件。遇到NULL时怎么写 checkbox单选 T4MVC Documentation page(转) ASIHTTPRequest 详解, http 请求终结者 (转) ASP.NET版Memcached监控工具(转) 技巧:使用User Control做HTML生成(转) Silverlight跟ASP.NET之間要如何溝通querystring(转) asp.net取得Silverlight中的值(转) IIS6 Silverlight部署經驗(转) c#四种eval方法(转) - WEBBER - 博客园 一行内文本超出指定宽度溢出的处理 sql2005数据同步技术(数据订阅,发布)转 C# 域用户操作(转) 看到了一些面试题,发给大家,看看多少人能都全回答上来 Membership角色与权限管理 (转) 我的跳槽求职经验谈(转) IHttpHandler的妙用(2):防盗链!我的资源只有我的用户才能下载 (转) JavaScript组件之JQuery(A~Z)教程(基于Asp.net运行环境)(转) 说说大型高并发高负载网站的系统架构 (转)
完整的DataTable和list转换 老外写的(转)
WEBBER · 2010-11-29 · via 博客园 - WEBBER

转自http://blog.csdn.net/educast/archive/2010/01/25/5256137.aspx

public class CollectionHelper  
{  
    private CollectionHelper()  
    {  
    }  
 
    public static DataTable ConvertTo<T>(IList<T> list)  
    {  
        DataTable table = CreateTable<T>();  
        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<T> ConvertTo<T>(IList<DataRow> rows)  
    {  
        IList<T> list = null;  
 
        if (rows != null)  
        {  
            list = new List<T>();  
 
            foreach (DataRow row in rows)  
            {  
                T item = CreateItem<T>(row);  
                list.Add(item);  
            }  
        }  
 
        return list;  
    }  
 
    public static IList<T> ConvertTo<T>(DataTable table)  
    {  
        if (table == null)  
        {  
            return null;  
        }  
 
        List<DataRow> rows = new List<DataRow>();  
 
        foreach (DataRow row in table.Rows)  
        {  
            rows.Add(row);  
        }  
 
        return ConvertTo<T>(rows);  
    }  
 
    public static T CreateItem<T>(DataRow row)  
    {  
        T obj = default(T);  
        if (row != null)  
        {  
            obj = Activator.CreateInstance<T>();  
 
            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<T>()  
    {  
        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<Customer> customers = new List<Customer>();  
 
        for (int i = 0; i < 10; i++)  
        {  
            Customer c = new Customer();  
            c.Id = i;  
            c.Name = "Customer " + i.ToString();  
 
            customers.Add(c);  
        }  
 
        DataTable table = CollectionHelper.ConvertTo<Customer>(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  
}