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

推荐订阅源

WordPress大学
WordPress大学
月光博客
月光博客
T
Tailwind CSS Blog
Y
Y Combinator Blog
L
Lohrmann on Cybersecurity
Latest news
Latest news
H
Heimdal Security Blog
MongoDB | Blog
MongoDB | Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
阮一峰的网络日志
阮一峰的网络日志
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
小众软件
小众软件
Security Archives - TechRepublic
Security Archives - TechRepublic
The Cloudflare Blog
T
Threatpost
S
Secure Thoughts
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
爱范儿
爱范儿
SecWiki News
SecWiki News
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
D
DataBreaches.Net
S
Security @ Cisco Blogs
B
Blog RSS Feed
N
News and Events Feed by Topic
V
Visual Studio Blog
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

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