DataTable 和 List 相互转化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataTable 和 List 相互转化相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Reflection;

/// <summary>
/// DtList 的摘要说明
/// </summary>
public class DtList
{
    /// <summary>  
    /// DataTable转换成List<T>  
    /// </summary>  
    /// <typeparam name="T"></typeparam>  
    /// <param name="table"></param>  
    /// <returns></returns>  
    public static List<T> GetList<T>(DataTable table)
    {
        List<T> list = new List<T>();
        T t = default(T);
        PropertyInfo[] propertypes = null;
        string tempName = string.Empty;
        foreach (DataRow row in table.Rows)
        {
            t = Activator.CreateInstance<T>();
            propertypes = t.GetType().GetProperties();
            foreach (PropertyInfo pro in propertypes)
            {
                tempName = pro.Name;
                if (table.Columns.Contains(tempName))
                {
                    object value = row[tempName];
                    if (!value.ToString().Equals(""))
                    {
                        pro.SetValue(t, value, null);
                    }
                }
            }
            list.Add(t);
        }
        return list.Count == 0 ? null : list;
    }

    /// <summary>  
    /// List<T>转换成DataSet  
    /// </summary>  
    /// <typeparam name="T"></typeparam>  
    /// <param name="list"></param>  
    /// <returns></returns>  
    public static DataSet ConvertToDataSet<T>(IList<T> list)
    {
        if (list == null || list.Count <= 0)
        {
            return null;
        }

        DataSet ds = new DataSet();
        DataTable dt = new DataTable(typeof(T).Name);
        DataColumn column;
        DataRow row;

        System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

        foreach (T t in list)
        {
            if (t == null)
            {
                continue;
            }

            row = dt.NewRow();

            for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
            {
                System.Reflection.PropertyInfo pi = myPropertyInfo[i];

                string name = pi.Name;

                if (dt.Columns[name] == null)
                {
                    column = new DataColumn(name, pi.PropertyType);
                    dt.Columns.Add(column);
                }

                row[name] = pi.GetValue(t, null);
            }

            dt.Rows.Add(row);
        }

        ds.Tables.Add(dt);

        return ds;
    }
}

 调用时:

//DataTable转化List
List<Model> list = DtList.GetList<Model>(dt);
//List转化DataSet
DataSet ds = DtList.ConvertToDataSet<Model>(list);

 

以上是关于DataTable 和 List 相互转化的主要内容,如果未能解决你的问题,请参考以下文章

DataTable 和List 相互转换

封装一个List集合和datatable相互转换的工具类

DataTable List 相互转换

List,泛型和Datatable 的相互转换

datatable list 之前相互转换

json与(Object/List/Map)的相互转化