DataTable转List,转对象

Posted plming

tags:

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

DataTable转List

技术分享图片
        public static List<T> ToListModel<T>(this DataTable table) where T : new()
        {
            var type = typeof(T);
            var properties = type.GetProperties();
            List<T> list = new List<T>();
            foreach (DataRow row in table.Rows)
            {
                var t = Activator.CreateInstance<T>();
                foreach (var p in properties)
                {
                    if (!table.Columns.Contains(p.Name))
                        continue;
                    if ((row[p.Name]).GetType() != typeof(DBNull))
                        p.SetValue(t, row[p.Name], null);
                }
                list.Add(t);
                t = default(T);
            }
            return list;
        }
View Code

DataTable转Object

技术分享图片
        public static object DataTableToObject(this DataTable table, int total)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
            data.Add("total", total);
            data.Add("rows", CreateRows(table));
            return data;   
        } 

        private static object CreateRows(DataTable table)
        {
            List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
            Dictionary<string, object> childRow;
            foreach (DataRow row in table.Rows)
            {
                childRow = new Dictionary<string, object>();
                foreach (DataColumn col in table.Columns)
                {
                    bool isDate = false;                    
                    #region 时间转化
                    if (col.ColumnName.Contains("_DATE") || col.ColumnName.Contains("_TIME") || col.ColumnName.Contains("_DT") || col.ColumnName.Contains("FDATE"))
                    {
                        isDate = true;
                    }                 
                    #endregion
                    if (isDate)
                    {
                        try
                        {
                            childRow.Add(col.ColumnName, row[col] is DBNull ? "" : Convert.ToDateTime(row[col]).ToString("yyyy-MM-dd"));
                        }
                        catch
                        {
                            childRow.Add(col.ColumnName, row[col]);
                        }
                    }
                    else {
                        childRow.Add(col.ColumnName, row[col]);
                    }
                }
                parentRow.Add(childRow);
            }
            return parentRow;
        } 
View Code

 

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

c#list与datatable 用哪个

ASP.NET中 datatable 和list的优劣处

c# DataTable转List 和 List转 DataTable

C#中list转table以及table转list

list转dataTable

C#DataTable(转List /JSON/字典 互转)