zoukankan      html  css  js  c++  java
  • c# list<class> to datatable

    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)
                        {
                            //if (DBNull.Value.Equals(pi.PropertyType))
                            //{
                            //   // pi.PropertyType = DateTime;
                            //}
                            Type colType = pi.PropertyType;
                            if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                            result.Columns.Add(pi.Name, colType);
                            //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;
            }
  • 相关阅读:
    Myeclipse10 + JBPM4.4 环境搭建图文教程
    关于ztree异步加载的问题(二)
    ztree学习之异步加载节点(一)
    【LeetCode】Search a 2D Matrix
    【LeetCode】Merge Sorted Array
    【LeetCode】Search for a Range
    一位阿里导师给大学生的忠告
    Java 基础
    java XML-RPC
    java web service
  • 原文地址:https://www.cnblogs.com/LiuFengH/p/13303928.html
Copyright © 2011-2022 走看看