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

推荐订阅源

Cloudbric
Cloudbric
Schneier on Security
Schneier on Security
V2EX - 技术
V2EX - 技术
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
O
OpenAI News
S
Security @ Cisco Blogs
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
Microsoft Azure Blog
Microsoft Azure Blog
Know Your Adversary
Know Your Adversary
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
Forbes - Security
Forbes - Security
NISL@THU
NISL@THU
Security Latest
Security Latest
G
Google Developers Blog
D
Docker
T
Threat Research - Cisco Blogs
N
Netflix TechBlog - Medium
C
CERT Recently Published Vulnerability Notes
H
Help Net Security
B
Blog
Martin Fowler
Martin Fowler
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
Lohrmann on Cybersecurity
Y
Y Combinator Blog
PCI Perspectives
PCI Perspectives
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
Project Zero
Project Zero
爱范儿
爱范儿
Cisco Talos Blog
Cisco Talos Blog
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
N
News | PayPal Newsroom
Recorded Future
Recorded Future

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