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

推荐订阅源

V
V2EX
W
WeLiveSecurity
IT之家
IT之家
A
About on SuperTechFans
B
Blog
L
LangChain Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
GbyAI
GbyAI
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
S
Security Affairs
The Last Watchdog
The Last Watchdog
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
V
Visual Studio Blog
H
Hacker News: Front Page
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Jina AI
Jina AI
N
News | PayPal Newsroom
S
Schneier on 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;
    }

}