List、DataTable和对象互转,List转DataTable异常Nullable解决方案

7 篇文章 0 订阅
7 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
 
namespace ClassLibrary1 {
    public class DataConvert {
 
        /// <summary>
        /// DataTable转换List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> TableToList<T>(DataTable dt) {
            List<T> ret = new List<T>();
            Type type = typeof(T);
            List<string> lstColumns = new List<string>();
            foreach (DataRow dr in dt.Rows) {
                PropertyInfo[] proInfo = type.GetProperties();
                T entity = Activator.CreateInstance<T>();
                foreach (PropertyInfo p in proInfo) {
                    if (!dt.Columns.Contains(p.Name) || dr[p.Name] == null || dr[p.Name] == DBNull.Value)
                        continue;
                    if (p.PropertyType == typeof(DateTime) && Convert.ToDateTime(dr[p.Name]) < Convert.ToDateTime("1753-01-01"))
                        continue;
                    try {
                        object obj = Convert.ChangeType(dr[p.Name], p.PropertyType);
                        p.SetValue(entity, obj, null);
                    } catch {
 
                    }
                }
                ret.Add(entity);
            }
            return ret;
        }
 
        /// <summary>
        /// List<T>转换DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable ListToTable<T>(List<T> list) {
            Type type = typeof(T);
            PropertyInfo[] proInfo = type.GetProperties();
            DataTable dt = new DataTable();
            foreach (PropertyInfo p in proInfo) {
                //类型存在Nullable<Type>时,需要进行以下处理,否则异常
                Type t = p.PropertyType;
                if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
                    t = t.GetGenericArguments()[0];
                dt.Columns.Add(p.Name, t);
            }
            foreach (T t in list) {
                DataRow dr = dt.NewRow();
                foreach (PropertyInfo p in proInfo) {
                    object obj = p.GetValue(t);
                    if (obj == null) continue;
                    if (p.PropertyType == typeof(DateTime) && Convert.ToDateTime(obj) < Convert.ToDateTime("1753-01-01"))
                        continue;
                    dr[p.Name] = obj;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
 
        public static T TableRowToEntity<T>(DataTable dt,int rowIndex) {
            Type type = typeof(T);
            T entity = Activator.CreateInstance<T>();
            if (dt == null) return entity;
            DataRow dr = dt.Rows[rowIndex];
            PropertyInfo[] proInfo = type.GetProperties();
            foreach (PropertyInfo p in proInfo) {
                if (!dt.Columns.Contains(p.Name) || dr[p.Name] == null || dr[p.Name] == DBNull.Value)
                    continue;
                if (p.PropertyType == typeof(DateTime) && Convert.ToDateTime(dr[p.Name]) < Convert.ToDateTime("1753-01-01"))
                    continue;
                try {
                    object obj = Convert.ChangeType(dr[p.Name], p.PropertyType);
                    p.SetValue(entity, obj, null);
                } catch{
 
                }
            }
            return entity;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值