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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 铭天

亲家网络的面试,请大家谨慎过去 redis命令String 安装redis RedHat6安装gcc Visual Studio 2015激活码,Visual Studio 2015密钥 从程序员到翻译的感受 取客户MAP地址 .net中的浅拷贝和深拷贝 DataReader转实体<T> 关于.Net类型转换 Jquery传数组数据到Mvc后台 Silverlight TreeView控件的增删改查 Visual Studio 2010序列号 SilverLight3访问Wcf - 铭天 - 博客园 访问Oracle11g的.net组件 js特效和树 自定义WebPart菜单 JSChart控件 & Ajax 图片 SilverLight2CheckBox动态调用WebService
DataTable到范型转换
铭天 · 2009-07-28 · via 博客园 - 铭天

/// <summary>
/// 适合于实体类和DataTable对应的情况
/// </summary>

public class ConvertExtend
{
    //将泛型类转换成DataTable
    public static DataTable ToDataTable<T>(List<T> entitys)
    {
        DataTable dtResult = new DataTable();
        if (entitys == null || entitys.Count < 1)
        {
            throw new Exception("需转换的集合为空");
        }
        Type entityType = entitys[0].GetType();
        List<PropertyInfo> propertyInfoList = entityType.GetProperties().ToList<PropertyInfo>();
        foreach (PropertyInfo item in propertyInfoList)
        {
            dtResult.Columns.Add(new DataColumn(item.Name, item.PropertyType));
        }
        foreach (T entity in entitys)
        {
            DataRow dr = dtResult.NewRow();
            foreach (PropertyInfo item in propertyInfoList)
            {
                dr[item.Name] = item.GetValue(entity, null);
            }
            dtResult.Rows.Add(dr);
        }
        return dtResult;
    }
    //将泛型类转换成DataTable
    public static IList<T> ToList<T>(DataTable dt)
    {
        List<T> list = new List<T>();
        //T model = default(T);
        for (int iRow = 0; iRow < dt.Rows.Count; iRow++)
        {
            DataRow dr = dt.Rows[iRow];
            T t = (T)System.Activator.CreateInstance(typeof(T));
            List<PropertyInfo> listPropertyInfo = t.GetType().GetProperties().ToList<PropertyInfo>();
            foreach (PropertyInfo item in listPropertyInfo)
            {
                if (dr[item.Name] != DBNull.Value)
                {
                    item.SetValue(t, dr[item.Name], null);
                }
                else
                {
                    item.SetValue(t, (string)null, null);
                }
            }
            list.Add(t);
        }
        return list;
    }

}