• List<T>转换为DataTable


    关于List<T>转换为DataTable的问题一直是开发过程中经常用到的,现从网上整理几个常用的转换方式,留作备用。

    (尊重作者原创,本文代码转发自 List 与 DataTable 转换  ) 

    [html] view plain copy
     
    1. public static class DataTableExtensions  
    2.    {  
    3.        /// <summary>  
    4.        /// 转化一个DataTable  
    5.        /// </summary>  
    6.        /// <typeparam name="T"></typeparam>  
    7.        /// <param name="list"></param>  
    8.        /// <returns></returns>  
    9.        public static DataTable ToDataTable<T>(this IEnumerable<T> list)  
    10.        {  
    11.            //创建属性的集合  
    12.            List<PropertyInfopList = new List<PropertyInfo>();  
    13.            //获得反射的入口  
    14.            Type type = typeof(T);  
    15.            DataTable dt = new DataTable();  
    16.            //把所有的public属性加入到集合 并添加DataTable的列  
    17.            Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });  
    18.            foreach (var item in list)  
    19.            {  
    20.                //创建一个DataRow实例  
    21.                DataRow row = dt.NewRow();  
    22.                //给row 赋值  
    23.                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));  
    24.                //加入到DataTable  
    25.                dt.Rows.Add(row);  
    26.            }  
    27.            return dt;  
    28.        }  
    29.    
    30.        /// <summary>  
    31.        /// DataTable 转换为List 集合  
    32.        /// </summary>  
    33.        /// <typeparam name="TResult">类型</typeparam>  
    34.        /// <param name="dt">DataTable</param>  
    35.        /// <returns></returns>  
    36.        public static List<T> ToList<T>(this DataTable dt) where T : class, new()  
    37.        {  
    38.            //创建一个属性的列表  
    39.            List<PropertyInfoprlist = new List<PropertyInfo>();  
    40.            //获取TResult的类型实例  反射的入口  
    41.            Type t = typeof(T);  
    42.            //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表   
    43.            Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });  
    44.            //创建返回的集合  
    45.            List<Toblist = new List<T>();  
    46.    
    47.            foreach (DataRow row in dt.Rows)  
    48.            {  
    49.                //创建TResult的实例  
    50.                T ob = new T();  
    51.                //找到对应的数据  并赋值  
    52.                prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });  
    53.                //放入到返回的集合中.  
    54.                oblist.Add(ob);  
    55.            }  
    56.            return oblist;  
    57.        }  
    58.    
    59.    
    60.        /// <summary>  
    61.        /// 将集合类转换成DataTable  
    62.        /// </summary>  
    63.        /// <param name="list">集合</param>  
    64.        /// <returns></returns>  
    65.        public static DataTable ToDataTableTow(IList list)  
    66.        {  
    67.            DataTable result = new DataTable();  
    68.            if (list.Count > 0)  
    69.            {  
    70.                PropertyInfo[] propertys = list[0].GetType().GetProperties();  
    71.                foreach (PropertyInfo pi in propertys)  
    72.                {  
    73.                    result.Columns.Add(pi.Name, pi.PropertyType);  
    74.                }  
    75.    
    76.                for (int i = 0; i list.Count; i++)  
    77.                {  
    78.                    ArrayList tempList = new ArrayList();  
    79.                    foreach (PropertyInfo pi in propertys)  
    80.                    {  
    81.                        object obj = pi.GetValue(list[i], null);  
    82.                        tempList.Add(obj);  
    83.                    }  
    84.                    object[] array = tempList.ToArray();  
    85.                    result.LoadDataRow(array, true);  
    86.                }  
    87.            }  
    88.            return result;  
    89.        }  
    90.    
    91.        /**/  
    92.        /// <summary>  
    93.        /// 将泛型集合类转换成DataTable  
    94.        /// </summary>  
    95.        /// <typeparam name="T">集合项类型</typeparam>  
    96.        /// <param name="list">集合</param>  
    97.        /// <returns>数据集(表)</returns>  
    98.        public static DataTable ToDataTable<T>(IList<T> list)  
    99.        {  
    100.            return ToDataTable<T>(list, null);  
    101.        }  
    102.    
    103.        /**/  
    104.        /// <summary>  
    105.        /// 将泛型集合类转换成DataTable  
    106.        /// </summary>  
    107.        /// <typeparam name="T">集合项类型</typeparam>  
    108.        /// <param name="list">集合</param>  
    109.        /// <param name="propertyName">需要返回的列的列名</param>  
    110.        /// <returns>数据集(表)</returns>  
    111.        public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)  
    112.        {  
    113.            List<stringpropertyNameList = new List<string>();  
    114.            if (propertyName != null)  
    115.                propertyNameList.AddRange(propertyName);  
    116.    
    117.            DataTable result = new DataTable();  
    118.            if (list.Count > 0)  
    119.            {  
    120.                PropertyInfo[] propertys = list[0].GetType().GetProperties();  
    121.                foreach (PropertyInfo pi in propertys)  
    122.                {  
    123.                    if (propertyNameList.Count == 0)  
    124.                    {  
    125.                        result.Columns.Add(pi.Name, pi.PropertyType);  
    126.                    }  
    127.                    else  
    128.                    {  
    129.                        if (propertyNameList.Contains(pi.Name))  
    130.                            result.Columns.Add(pi.Name, pi.PropertyType);  
    131.                    }  
    132.                }  
    133.    
    134.                for (int i = 0; i list.Count; i++)  
    135.                {  
    136.                    ArrayList tempList = new ArrayList();  
    137.                    foreach (PropertyInfo pi in propertys)  
    138.                    {  
    139.                        if (propertyNameList.Count == 0)  
    140.                        {  
    141.                            object obj = pi.GetValue(list[i], null);  
    142.                            tempList.Add(obj);  
    143.                        }  
    144.                        else  
    145.                        {  
    146.                            if (propertyNameList.Contains(pi.Name))  
    147.                            {  
    148.                                object obj = pi.GetValue(list[i], null);  
    149.                                tempList.Add(obj);  
    150.                            }  
    151.                        }  
    152.                    }  
    153.                    object[] array = tempList.ToArray();  
    154.                    result.LoadDataRow(array, true);  
    155.                }  
    156.            }  
    157.            return result;  
    158.        }  
    159.    }  

    前两种方式通过集合的 FOREACH 方法,获取泛型类型的属性和类型初始化转换的对象;

    后两种为显示遍历转换源集合的属性和类型 ,然后逐一将数据更新的目的集合中。

    附 foreach 方法说明:

    C# 语言的 foreach 语句(在 Visual Basic 中为 For Each)隐藏了枚举数的复杂性。因此,建议使用 foreach,而不直接操作枚举数。

    枚举数可用于读取集合中的数据,但不能用于修改基础集合。

    最初,枚举数定位在集合中第一个元素前。Reset 方法还会将枚举数返回到此位置。在此位置上,Current 属性未定义。因此,在读取 Current 的值之前,必须调用 MoveNext 方法将枚举数提前到集合的第一个元素。

    在调用 MoveNext 或 Reset 之前,Current 返回同一对象。MoveNext 将 Current 设置为下一个元素。

    如果 MoveNext 越过集合的末尾,则枚举数将被放置在此集合中最后一个元素的后面,而且 MoveNext 返回 false。当枚举数位于此位置时,对 MoveNext 的后续调用也返回 false。如果上一个 MoveNext 调用返回 false,则 Current 未定义。若要再次将 Current 设置为集合的第一个元素,可以调用 Reset,然后再调用 MoveNext。

    只要集合保持不变,枚举数就保持有效。如果对集合进行更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,而且其行为是不确定的。

    枚举数没有对集合的独占访问权;因此,枚举通过集合在本质上不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

  • 相关阅读:
    测试开发面试集锦_数据库
    测试开发面试集锦_linux
    测试开发面试题集锦_java
    Java中equals 和==的区别
    定时清理文件shell脚本
    java文件上传,upload使用
    python 获取错误日志,并发送邮件
    c语言代码审计规范
    渗透测试之nmap
    渗透测试之GoogleHack
  • 原文地址:https://www.cnblogs.com/sjd1118/p/7269311.html
Copyright © 2020-2023  润新知