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”开始的
  • 相关阅读:
    iOS 获取项目名称及版本号
    Xcode 使用自定义字体
    自己制作精美的App Store 软件截屏
    iOS 数据持久性存储-对象归档
    iOS 数据持久性存储-属性列表
    好长时间没回来了,回归本位!
    VBA删除表格最后一行
    VBA表格单元格替换文字
    VBA添加表格
    oracle语句
  • 原文地址:https://www.cnblogs.com/wxxf/p/14846447.html
Copyright © 2011-2022 走看看