• 将IList<T>转换成DataTable


    public static DataTable Convert<T>(IList<T> list)
            {
                if (list == null || list.Count <= 0)
                {
                    return null;
                }
                DataTable dt = new DataTable(typeof(T).Name);
                DataColumn dc;
                DataRow dr;

                PropertyInfo[] propertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (T t in list)
                {
                    if (t == null)
                    {
                        continue;
                    }
                    dr = dt.NewRow();
                    for (int i = 0, j = propertyInfo.Length; i < j; i++)
                    {
                        PropertyInfo pi = propertyInfo[i];
                        string name = pi.Name;
                        if (dt.Columns[name] == null)
                        {
                            dc = new DataColumn(name, pi.PropertyType);
                            dt.Columns.Add(dc);
                        }
                        dr[name] = pi.GetValue(t, null);
                    }
                    dt.Rows.Add(dr);
                }
                return dt;
            }

  • 相关阅读:
    SQL操作符的优化
    Oracle 模糊查询 优化
    Mysql中的语句优化
    SQL优化
    Pro Git读书笔记
    前端工程化
    前端工程化
    前端工程化
    前端工程化
    前端工程化
  • 原文地址:https://www.cnblogs.com/xiaozhuaweiliang/p/DataTable.html
Copyright © 2020-2023  润新知