zoukankan      html  css  js  c++  java
  • 将集合类转换成DataTable dodo

             /// 将集合类转换成DataTable
            /// </summary>
            /// <param name="list">集合</param>
            /// <returns></returns>
            public static DataTable ToDataTable(IList list)
            {
                DataTable result = new DataTable();
                if (list.Count > 0)
                {
                    PropertyInfo[] propertys = list[0].GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {

                        Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) //避免空值报错
                        {

                            colType = colType.GetGenericArguments()[0];

                        }

                        if (pi.Name != "DataTable_Action_")  //避免列要求有效的 DataType错误,主要是针对自定义数据类型
                        {

                            result.Columns.Add(new DataColumn(pi.Name, colType));
                        }

                    }

                    for (int i = 0; i < list.Count; i++)
                    {
                        ArrayList tempList = new ArrayList();
                        foreach (PropertyInfo pi in propertys)
                        {
                            if (pi.Name != "DataTable_Action_")//避免列要求有效的 DataType错误,主要是针对自定义数据类型

                            {
                                object obj = pi.GetValue(list[i], null) == null ? DBNull.Value : pi.GetValue(list[i], null); //避免空值报错

                                //object obj = pi.GetValue(list[i], null);
                                tempList.Add(obj);
                            }
                        }
                        object[] array = tempList.ToArray();
                        result.LoadDataRow(array, true);
                    }
                }
                return result;
            }

  • 相关阅读:
    19年下半年读书清单一览
    2019-2020:时间戳
    全链路压测资料汇总——业内大厂解决方案
    个人公众号开通啦
    windows 10环境下安装Tensorflow-gpu
    如何判断安卓模拟器的型号(品牌)
    socket.io的websocket示例
    Node + Selenium使用小结
    基于SOUI开发一个简单的小工具
    国际化之Android设备支持的语种
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/2128957.html
Copyright © 2011-2022 走看看