码迷,mamicode.com
首页 > 其他好文 > 详细

DataTable转List,转对象

时间:2017-12-29 10:17:14      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:mode   instance   img   null   closed   convert   tostring   aaa   oda   

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,转对象

标签:mode   instance   img   null   closed   convert   tostring   aaa   oda   

原文地址:https://www.cnblogs.com/plming/p/8142931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!