zoukankan      html  css  js  c++  java
  • IList,List<T>转换为DataTable 常用收藏

        List<info> infos = Dal.GetInfos();  
                DataTable dt = new DataTable();  
                dt.Columns.Add("cName");  
          
                foreach (var info in infos)  
                {  
                    DataRow dr = dt.NewRow();  
                    dr["cName"] = info.Name;  
                    dt.Add(dr);  
                }  
    View Code
        public static class DataTableExtensions  
        {  
            /// <summary>    
          
            /// 转化一个DataTable    
          
            /// </summary>    
          
            /// <typeparam name="T"></typeparam>    
            /// <param name="list"></param>    
            /// <returns></returns>    
            public static DataTable ToDataTable<T>(this IEnumerable<T> list)  
            {  
          
                //创建属性的集合    
                List<PropertyInfo> pList = new List<PropertyInfo>();  
                //获得反射的入口    
          
                Type type = typeof(T);  
                DataTable dt = new DataTable();  
                //把所有的public属性加入到集合 并添加DataTable的列    
                Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });  
                foreach (var item in list)  
                {  
                    //创建一个DataRow实例    
                    DataRow row = dt.NewRow();  
                    //给row 赋值    
                    pList.ForEach(p => row[p.Name] = p.GetValue(item, null));  
                    //加入到DataTable    
                    dt.Rows.Add(row);  
                }  
                return dt;  
            }  
          
          
            /// <summary>    
            /// DataTable 转换为List 集合    
            /// </summary>    
            /// <typeparam name="TResult">类型</typeparam>    
            /// <param name="dt">DataTable</param>    
            /// <returns></returns>    
            public static List<T> ToList<T>(this DataTable dt) where T : class, new()  
            {  
                //创建一个属性的列表    
                List<PropertyInfo> prlist = new List<PropertyInfo>();  
                //获取TResult的类型实例  反射的入口    
          
                Type t = typeof(T);  
          
                //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表     
                Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });  
          
                //创建返回的集合    
          
                List<T> oblist = new List<T>();  
          
                foreach (DataRow row in dt.Rows)  
                {  
                    //创建TResult的实例    
                    T ob = new T();  
                    //找到对应的数据  并赋值    
                    prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });  
                    //放入到返回的集合中.    
                    oblist.Add(ob);  
                }  
                return oblist;  
            }  
          
          
          
          
            /// <summary>    
            /// 将集合类转换成DataTable    
            /// </summary>    
            /// <param name="list">集合</param>    
            /// <returns></returns>    
            public static DataTable ToDataTableTow(IList list)  
            {  
                DataTable result = new DataTable();  
                if (list.Count > 0)  
                {  
                    PropertyInfo[] propertys = list[0].GetType().GetProperties();  
          
                    foreach (PropertyInfo pi in propertys)  
                    {  
                        result.Columns.Add(pi.Name, pi.PropertyType);  
                    }  
                    for (int i = 0; i < list.Count; i++)  
                    {  
                        ArrayList tempList = new ArrayList();  
                        foreach (PropertyInfo pi in propertys)  
                        {  
                            object obj = pi.GetValue(list[i], null);  
                            tempList.Add(obj);  
                        }  
                        object[] array = tempList.ToArray();  
                        result.LoadDataRow(array, true);  
                    }  
                }  
                return result;  
            }  
          
          
            /**/  
          
            /// <summary>    
            /// 将泛型集合类转换成DataTable    
          
            /// </summary>    
            /// <typeparam name="T">集合项类型</typeparam>    
          
            /// <param name="list">集合</param>    
            /// <returns>数据集(表)</returns>    
            public static DataTable ToDataTable<T>(IList<T> list)  
            {  
                return ToDataTable<T>(list, null);  
          
            }  
          
          
            /**/  
          
            /// <summary>    
            /// 将泛型集合类转换成DataTable    
            /// </summary>    
            /// <typeparam name="T">集合项类型</typeparam>    
            /// <param name="list">集合</param>    
            /// <param name="propertyName">需要返回的列的列名</param>    
            /// <returns>数据集(表)</returns>    
            public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)  
            {  
                List<string> propertyNameList = new List<string>();  
                if (propertyName != null)  
                    propertyNameList.AddRange(propertyName);  
                DataTable result = new DataTable();  
                if (list.Count > 0)  
                {  
                    PropertyInfo[] propertys = list[0].GetType().GetProperties();  
                    foreach (PropertyInfo pi in propertys)  
                    {  
                        if (propertyNameList.Count == 0)  
                        {  
                            result.Columns.Add(pi.Name, pi.PropertyType);  
                        }  
                        else  
                        {  
                            if (propertyNameList.Contains(pi.Name))  
                                result.Columns.Add(pi.Name, pi.PropertyType);  
                        }  
                    }  
          
                    for (int i = 0; i < list.Count; i++)  
                    {  
                        ArrayList tempList = new ArrayList();  
                        foreach (PropertyInfo pi in propertys)  
                        {  
                            if (propertyNameList.Count == 0)  
                            {  
                                object obj = pi.GetValue(list[i], null);  
                                tempList.Add(obj);  
                            }  
                            else  
                            {  
                                if (propertyNameList.Contains(pi.Name))  
                                {  
                                    object obj = pi.GetValue(list[i], null);  
                                    tempList.Add(obj);  
                                }  
                            }  
                        }  
                        object[] array = tempList.ToArray();  
                        result.LoadDataRow(array, true);  
                    }  
                }  
                return result;  
            }  
        }  
    View Code
  • 相关阅读:
    python制作一个塔防射箭游戏
    有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同 学号的那些结点。
    python实现一个简单的21点游戏
    C语音,函数padd的功能是调整pa指向的链表中结点的位置,使得所有x值为偶数的结点出现在链表的前半部,所有x值为奇数的结点出现在链表的后半部。
    scratch绘制特殊图形1
    验证哥德巴赫猜想,输出6-100之间的偶数等于两个质数之和
    写一函数check检测字符串中的左右括号数是否匹配
    C语言文件操作题,将整数1-10及其算术平方根存入文件,再读取出来显示在屏幕上
    湖南2020对口计算机32题第1、2、3小题
    基础
  • 原文地址:https://www.cnblogs.com/myloveblogs/p/5129150.html
Copyright © 2011-2022 走看看