ListToDataTable

本文介绍了一种将C#中的泛型列表转换为DataTable的方法。此方法通过反射获取列表中对象的属性,并创建相应的DataTable列,然后填充数据行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 public static DataTable ToDataTable<T>(this IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();
            // column names 
            PropertyInfo[] oProps = null;
            if (varlist == null)
                return dtReturn;
            foreach (T rec in varlist)
            {
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                    }
                }
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue

                    (rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;
        }

要将List转换为DataTable,可以使用以下代码实现: ``` public DataTable ListToDataTable<T>(List<T> list) { DataTable dataTable = new DataTable(); PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { dataTable.Columns.Add(property.Name, property.PropertyType); } foreach (T item in list) { DataRow dataRow = dataTable.NewRow(); foreach (PropertyInfo property in properties) { dataRow[property.Name = property.GetValue(item); } dataTable.Rows.Add(dataRow); } return dataTable; } ``` 这个方法接受一个List作为参数,并使用反射来获取List中对象的属性信息。然后,它创建一个DataTable对象,并根据属性信息添加列。接下来,它遍历List中的每个对象,创建一个DataRow对象,并将属性值赋给相应的列。最后,将DataRow添加到DataTable中,最终返回转换后的DataTable。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [List转DataTable并导出为Excel](https://download.csdn.net/download/vouyv/9512300)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [DataTable转换为List的通用方法](https://blog.csdn.net/runAndRun/article/details/12519663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值