当前位置: 移动技术网 > IT编程>开发语言>.net > C# 把List 集合转换成DataTable

C# 把List 集合转换成DataTable

2020年10月10日  | 移动技术网IT编程  | 我要评论
/// <summary> /// 将List集合 转换成 DataTable /// </summary> /// <param name="entitys">sellerSearchDealList是我自己定义的一个类</param> public static DataTable ListToTable(List<object> entitys) {
/// <summary>
        /// 将List集合 转换成 DataTable
        /// </summary>
        /// <param name="entitys">sellerSearchDealList是我自己定义的一个类</param>
        public static DataTable ListToTable(List<object> entitys)
        {
            DataTable dtresult = new DataTable();
            if (entitys == null || entitys.Count < 1)
            {
                throw new Exception("空");
            }
            else
            {
                PropertyInfo[] propertys = entitys[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    dtresult.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < entitys.Count; i++)
                {
                    ArrayList tenpList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(entitys[i], null);
                        tenpList.Add(obj);
                    }
                    object[] array = tenpList.ToArray();
                    dtresult.LoadDataRow(array, true);
                }
            }
            return dtresult;
        }

本文地址:https://blog.csdn.net/weixin_40362806/article/details/108992669

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网