C#中List与DataTable相互转换

DataTable是C#语言中一个独特而又强大的数据类型。若想了解更多,请自行百度去吧...

下面提供一下List集合数据类型与DataTable表格数据类型的相互转换。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace JR_Common
{
    /// 
    /// List与DataTable相互转换的工具类
    /// 
    public class ListTable
    {

        /// 
        /// DataTable对象转换成List对象
        /// 
        /// 
        /// 
        /// 
        public static List DataTableToList(DataTable table)
        {
            if (table == null)
            {
                return null;
            }

            List rows = new List();

            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            List list = null;

            if (rows != null)
            {
                list = new List();

                foreach (DataRow row in rows)
                {
                    T item = DataRowToObject(row);
                    list.Add(item);
                }
            }

            return list;
        }


        private static List ConvertTo(List rows)
        {
            List list = null;

            if (rows != null)
            {
                list = new List();

                foreach (DataRow row in rows)
                {
                    T item = DataRowToObject(row);
                    list.Add(item);
                }
            }

            return list;
        }


        /// 
        /// DataRow对象转换成Object对象
        /// 
        /// 
        /// 
        /// 
        public static T DataRowToObject(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue(obj, value);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return obj;
        }


        /// 
        /// List对象转换成DataTable对象
        /// 
        /// 
        /// 
        /// 
        public static DataTable ListToDataTable(List collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }




    }
}
希望给你带来帮助!!!谢谢大家。

你可能感兴趣的:(编程语言)