DataTable类型对象转换为List集合

本文提供了一个实用的方法,用于将DataTable对象转换成泛型List集合。该方法适用于.NET开发中数据处理的需求,尤其对于需要进行数据转换的场景非常有用。

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

namespace ChatRoom.Services.Utils
{
    public static class ConvertUtils
    {
        /// <summary>
        /// 提供将DataTable类型对象转换为List集合
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<T> ConvertToList<T>(DataTable table) where T : new()
        {
            //置为垃圾对象
            List<T> list = null;

            if (table != null)
            {
                DataColumnCollection columns = table.Columns;
                int columnCount = columns.Count;
                T type = new T();
                Type columnType = type.GetType();
                PropertyInfo[] properties = columnType.GetProperties();

                if (properties.Length == columnCount)
                {
                    list = new List<T>();
                    foreach (DataRow currentRow in table.Rows)
                    {
                        for (int i = 0; i < columnCount; i++)
                        {
                            for (int j = 0; j < properties.Length; j++)
                            {
                                if (columns[i].ColumnName == properties[j].Name)
                                {
                                    properties[j].SetValue(type, currentRow[i], null);
                                }
                            }
                        }
                        list.Add(type);
                        type = new T();
                    }
                }
                else
                {
                    list = null;
                }
            }
            else
            {
                throw new ArgumentNullException("参数不能为空");
            }
            return list;
        }
    }
}


不知效率性能如何啊??

DataTable转换List集合是一个常见的编程任务,特别是在.NET框架中,DataTable通常用来表示内存中的数据表。下面是一个如何实现这种转换的步骤说明: 1. 创建DataTable对象并填充数据。 2. 定义一个与DataTable中行结构相对应的类(Poco类)。 3. 使用反射、Linq或其他方法遍历DataTable的每一行,并将其转换为Poco类的实例。 4. 将这些实例添加到List集合中。 以下是使用C#中的Linq to Dataset功能的示例代码: ```csharp using System; using System.Collections.Generic; using System.Data; using System.Linq; public class DataEntity { // 假设你的DataTable中有Name和Age两列 public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { // 创建一个DataTable并添加列和行数据 DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Age", typeof(int)); dt.Rows.Add("Alice", 25); dt.Rows.Add("Bob", 22); dt.Rows.Add("Charlie", 30); // 使用Linq将DataTable转换List<DataEntity> List<DataEntity> dataList = dt.AsEnumerable() .Select(row => new DataEntity { Name = row.Field<string>("Name"), Age = row.Field<int>("Age") }) .ToList(); // 此时dataList包含了DataTable中的所有数据 } } ``` 在这个示例中,我们首先定义了一个名为`DataEntity`的类,它具有与DataTable中列对应的属性。然后我们创建了一个DataTable并添加了一些数据。使用Linq的`AsEnumerable()`方法将DataTable转换为可枚举类型,然后使用`Select`方法将每一行转换为`DataEntity`类的实例,并最终调用`ToList()`方法将结果转换List集合
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值