list集合转换成datatable

 /// 将list集合转换成datatable
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static System.Data.DataTable ListToDataTable(IList list)
        {
            System.Data.DataTable result = new System.Data.DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    //获取类型
                    Type colType = pi.PropertyType;
                    //当类型为Nullable<>时
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    result.Columns.Add(pi.Name, colType);
                }
                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }

原文地址:https://www.cnblogs.com/macT/p/10214374.html

时间: 2024-10-10 18:47:19

list集合转换成datatable的相关文章

讲List转换成DataTable

/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list"></param> /// <returns></returns> public static System.Data.DataTable ListToDataTable(IList list) { System.Data.DataTable result = new

C#_List转换成DataTable

/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list"></param> /// <returns></returns> public static System.Data.DataTable ListToDataTable(IList list) { System.Data.DataTable result = new

将泛类型集合List类转换成DataTable

/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list">泛类型集合</param> /// <returns></returns> public static DataTable ListToDataTable<T>(List<T> entitys) { //检查实体集合不能为空 if (

list 转换成datatable

感谢网上的一位朋友 1 /// <summary> 2 60 /// 将集合类转换成DataTable 3 61 /// </summary> 4 62 /// <param name="list">集合</param> 5 63 /// <returns></returns> 6 64 public static DataTable ToDataTable(IList list) 7 65 { 8 66 Data

将list&lt;对象&gt;转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据

领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其他的方法,不过这个方法已经实现,就写一下了: 1.创建表. CREATE TABLE [dbo].[person]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [Pwd] [nvarchar](50) NULL, [Age]

List转DataTable(反射) ; 将泛型集合类转换成DataTable ; 将集合类转换成DataTable

#region 将集合类转换成DataTable /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param name="list">集合</param> /// <returns></returns> public static DataTable ToDataTable(IList list) { DataTable result = new Dat

将List集合类转换成DataTable

/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list">泛类型集合</param> /// <returns></returns> public static DataTable ListToDataTable<T>(List<T> entitys) { //检查实体集合不能为空 if (

C# DataTable转换成实体列表 与 实体列表转换成DataTable

/// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T">实体 T </typeparam> /// <param name="table">datatable</param> /// <returns></returns> public static IList<T>

.net excel 转换成datatable,创建文件夹

protected void Button9_Click(object sender, EventArgs e) { string path = ""; path = FileUpload3.PostedFile.FileName; if (path == "") { string jss = "<script language='javascript' type='text/javascript'> alert('先选择文件')</sc