• 将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;
            }

  • 相关阅读:
    awk统计命令(求和、求平均、求最大值、求最小值)(转)
    高性能跨平台网络IO(Reactor、epoll、iocp)总结
    进程通信和同步(转)
    C++11原子操作与无锁编程(转)
    在线代码编译运行工具
    linux ps 命令的查看
    转: linux sed 命令的使用
    转:利用Eclipse CDT 阅读C/C++代码
    转:Raft一致性选举算法的ppt与视频
    转:ffmpeg time_base详解
  • 原文地址:https://www.cnblogs.com/xiaozhuaweiliang/p/DataTable.html
Copyright © 2020-2023  润新知