前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# DataTable 转换成List<T>

C# DataTable 转换成List<T>

作者头像
aehyok
发布2018-09-11 12:03:29
1.9K0
发布2018-09-11 12:03:29
举报
文章被收录于专栏:技术博客技术博客
代码语言:javascript
复制
运用泛型和反射实现的转换,很给力。代码中掺杂详尽注释,稍微了解一下泛型和反射便可以了解转换的实质。可以直接复制粘贴进行调用哦。

public class DtConverToList<T> where T : new()
{
    public static List<T> DtToList(DataTable dt)
    {
        //定义集合
        List<T> ListCollection = new List<T>(dt.Rows.Count);
        //获得 T 模型类型
        Type T_type = typeof(T);
        //获得 T 模型类型公共属性
        PropertyInfo[] Proper = T_type.GetProperties();
        //临时变量,存储变量模型公共属性Name
        string Tempname = "";
        //遍历参数 DataTable的每行
        foreach (DataRow Dr in dt.Rows)
        {
            //实例化 T 模版类
            T t = new T();
            //遍历T 模版类各个属性
            #region
            foreach (PropertyInfo P in Proper)
            {
                //取出类属性之一
                Tempname = P.Name;
                //判断DataTable中是否有此列
                if (dt.Columns.Contains(Tempname))
                {
                    //判断属性是否可写属性
                    if (!P.CanWrite)
                    {
                        continue;
                    }
                    try
                    {
                        //得到Datable单元格中的值
                        object value = Dr[Tempname];
                        //得到 T 属性类型
                        Type ProType = P.PropertyType;
                        //判断类型赋值
                        if (value != DBNull.Value)
                        {
                            //
                            if (value.GetType() == ProType)
                            {
                                P.SetValue(t, value, null);
                            }
                            else
                            {
                                if (ProType == typeof(string))
                                {
                                    string Temp = value.ToString();
                                    P.SetValue(t, Temp, null);
                                }
                                else if (ProType == typeof(byte))
                                {
                                    byte Temp = Convert.ToByte(value);
                                    P.SetValue(t, Temp, null);
                                }
                                else if (ProType == typeof(short))
                                {
                                    short Temp = short.Parse(value.ToString());
                                    P.SetValue(t, Temp, null);
                                }
                                else if (ProType == typeof(long))
                                {
                                    long Temp = long.Parse(value.ToString());
                                    P.SetValue(t, Temp, null);
                                }
 
                                else if (ProType == typeof(Int64))
                                {
                                    Int64 Temp = Convert.ToInt64(value);
                                    P.SetValue(t, Temp, null);
                                }
                                else if (ProType == typeof(Int32))
                                {
                                    Int32 Temp = Convert.ToInt32(value);
                                    P.SetValue(t, Temp, null);
                                }
                                else if (ProType == typeof(Int16))
                                {
                                    Int16 Temp = Convert.ToInt16(value);
                                    P.SetValue(t, Temp, null);
                                }
                                else
                                {
                                    object Temp = Convert.ChangeType(value, ProType);
                                    P.SetValue(t, Temp, null);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
 
                        throw;
                    }
                }
            }
            #endregion
            ListCollection.Add(t);
        }
        return ListCollection;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-03-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档