zoukankan      html  css  js  c++  java
  • List与DataTable相互转换

       /// <summary>
            /// 实体转换
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <typeparam name="U"></typeparam>
            /// <param name="tolist"></param>
            /// <param name="fromlist"></param>
            /// <returns></returns>
            public static T AutoMap<T,U>(T tolist,U fromlist)
            {
                //返回当前对象的所有公共属性
                PropertyInfo[] topropertys = typeof(T).GetProperties();
                PropertyInfo[] frompropertys = typeof(U).GetProperties();
                foreach(PropertyInfo toproperty in topropertys)
                {
                    foreach(PropertyInfo fromproperty in frompropertys)
                    {
                        toproperty.SetValue(tolist,fromproperty.GetValue(fromlist));
                    }
                }
                return tolist;
            }  
    
    /// <summary>
            /// datatable转list
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
            {
                List<Dictionary<string, object>> list
                     = new List<Dictionary<string, object>>();
    
                foreach (DataRow dr in dt.Rows)
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        dic.Add(dc.ColumnName, dr[dc.ColumnName]);
                    }
                    list.Add(dic);
                }
                return list;
            }
    
            /// <summary>
            /// list 转datatable
            /// </summary>
            private static DataTable ToDataTable<T>(List<T> list)
            {
                var tb = new DataTable(typeof(T).Name);
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                //表列名
                foreach (PropertyInfo prop in props)
                {
                    Type t = GetCoreType(prop.PropertyType);
                    tb.Columns.Add(prop.Name, t);
                }
                //遍历数据
                foreach (T item in list)
                {
                    var values = new object[props.Length];
    
                    for (int i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                    }
    
                    tb.Rows.Add(values);
                }
    
                return tb;
            }
            /// <summary>
            /// 如果类型可以为空,则返回基础类型,否则返回类型
            /// </summary>
            public static Type GetCoreType(Type t)
            {
                if (t != null && IsNullable(t))
                {
                    if (!t.IsValueType)
                    {
                        return t;
                    }
                    else
                    {
                        return Nullable.GetUnderlyingType(t);
                    }
                }
                else
                {
                    return t;
                }
            }
            /// <summary>
            /// 指定类型可为空
            /// </summary>
            public static bool IsNullable(Type t)
            {
                return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
            }
    无穷的伟大,也是从“0”开始的
  • 相关阅读:
    Docker学习-安装,配置,运行
    Docker学习-从无知到有知的学习过程
    学习记录-java基础部分(一)
    对get post等http请求方式的理解
    Mac和window实现双向数据传输
    git pull时 git cannot lock ref XXXXXX (unable to update local ref)错误解决方案
    三年内我的计划和方向
    关于云服务器和云部署的实操(新手级别入门)
    win7蓝屏死机0x0000003B错误蓝屏故障解决
    JAVA代码:生成一个集合,自定义大小,100以内的随机整数
  • 原文地址:https://www.cnblogs.com/wxxf/p/14846447.html
Copyright © 2011-2022 走看看