Net Core教程

C# DataTable 互转 List<T>

本文主要是介绍C# DataTable 互转 List<T>,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 添加帮助类 DataTableHelper.cs

public static class DataTableHelper
{
    /// <summary>
    /// List转DataTable
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    public static DataTable ConvertTo<T>(IList<T> list)
    {
        DataTable table = CreateTable<T>();
        Type entityType = typeof(T);
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
        foreach (T item in list)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item);
            table.Rows.Add(row);
        }
        return table;
    }

    private static DataTable CreateTable<T>()
    {
        Type entityType = typeof(T);
        DataTable table = new DataTable(entityType.Name);
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, prop.PropertyType);

        return table;
    }

    /// <summary>
    /// DataTable转List
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="table"></param>
    /// <returns></returns>
    public static IList<T> ConvertTo<T>(DataTable table)
    {
        if (table == null)
            return null;

        List<DataRow> rows = new List<DataRow>();
        foreach (DataRow row in table.Rows)
            rows.Add(row);

        return ConvertTo<T>(rows);
    }

    private static IList<T> ConvertTo<T>(IList<DataRow> rows)
    {
        IList<T> list = null;
        if (rows != null)
        {
            list = new List<T>();
            foreach (DataRow row in rows)
            {
                T item = CreateItem<T>(row);
                list.Add(item);
            }
        }
        return list;
    }

    //将数据行转换为T对象
    private static T CreateItem<T>(DataRow row)
    {
        string columnName;
        T obj = default(T);
        if (row != null)
        {
            obj = Activator.CreateInstance<T>();
            foreach (DataColumn column in row.Table.Columns)
            {
                columnName = column.ColumnName;
                //获取具有相同列名的属性
                PropertyInfo prop = obj.GetType().GetProperty(columnName);
                try
                {
                    //获取列的值
                    object value = (row[columnName].GetType() == typeof(DBNull)) ? null : row[columnName];
                    //设置属性值
                    if (prop.CanWrite)    //判断其是否可写
                        prop.SetValue(obj, value, null);
                }
                catch { }
            }
        }
        return obj;
    }
}

 

2. 创建实体类

public class User
{
    public string ID { get; set; }
    public string Name { get; set; }
}

 

3. 实现

DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add("1", "哈哈哈");
dt.Rows.Add("2", "嘎嘎");
IList<User> list = DataTableHelper.ConvertTo<User>(dt);
DataTable dt1 = DataTableHelper.ConvertTo<User>(list);

 

这篇关于C# DataTable 互转 List<T>的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!