zoukankan      html  css  js  c++  java
  • c#将List<T>转换成DataSet

            /// <summary>
            /// List<T> 转换成DataSet
            /// </summary>
            /// <typeparam name="T">对象</typeparam>
            /// <param name="list">集合</param>
            /// <returns>DataSet</returns>
            public static DataSet ConvertToDataSet<T>(List<T> list)
            {
                if (list == null || list.Count <= 0)
                {
                    return null;
                }

                DataSet ds = new DataSet();
                DataTable dt = new DataTable(typeof(T).Name);
                DataColumn column;
                DataRow row;

                System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

                foreach (T t in list)
                {
                    if (t == null)
                    {
                        continue;
                    }

                    row = dt.NewRow();

                    for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                    {
                        System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                        string name = pi.Name;

                        if (dt.Columns[name] == null)
                        {
                            column = new DataColumn(name, pi.PropertyType);
                            dt.Columns.Add(column);
                        }

                        row[name] = pi.GetValue(t, null);
                    }

                    dt.Rows.Add(row);
                }
                ds.Tables.Add(dt);
                return ds;
            }
        }

  • 相关阅读:
    Insubstantial 6.2 Release
    解决异常:Package should contain a content type part [M1.13]
    Peer-to-Peer 综述
    P2P网络穿越 NAT穿越
    Faster_RCNN 2.模型准备(上)
    Pytorch Visdom
    python opencv3添加opencv-contrib
    Pytorch之验证码识别
    Pytorch tutorial 之Datar Loading and Processing (2)
    Pytorch tutorial 之Datar Loading and Processing (1)
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7044636.html
Copyright © 2011-2022 走看看