C# DataTable 互转 List

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);

 

上一篇:jQuery购物车数量加减小功能


下一篇:Attr和Prop的区别