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

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LangChain Blog
博客园_首页
Jina AI
Jina AI
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tenable Blog
量子位
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Security Affairs
Last Week in AI
Last Week in AI
Scott Helme
Scott Helme
月光博客
月光博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
Simon Willison's Weblog
Simon Willison's Weblog
PCI Perspectives
PCI Perspectives
人人都是产品经理
人人都是产品经理
N
News and Events Feed by Topic
腾讯CDC
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
博客园 - 司徒正美
博客园 - Franky
Latest news
Latest news
S
SegmentFault 最新的问题
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
H
Heimdal Security Blog
H
Hacker News: Front Page
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
T
Troy Hunt's Blog
N
News | PayPal Newsroom
P
Palo Alto Networks Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
雷峰网
雷峰网

博客园 - 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  
}