zoukankan      html  css  js  c++  java
  • 类型转换

    #region List <=> DataTable 互相转换
    
            /// <summary> 
            /// LINQ返回DataTable类型
            /// </summary> 
            /// <typeparam name="T"> </typeparam> 
            /// <param name="varlist"> </param> 
            /// <returns> </returns> 
    
            public static DataTable ListToDataTable<T>(IEnumerable<T> varlist)
            {
    
                DataTable dtReturn = new DataTable();
                // column names 
                PropertyInfo[] oProps = null;
                if (varlist == null)
                    return dtReturn;
    
                foreach (T rec in varlist)
                {
                    if (oProps == null)
                    {
                        oProps = ((Type)rec.GetType()).GetProperties();
                        foreach (PropertyInfo pi in oProps)
                        {
                            Type colType = pi.PropertyType;
                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                                 == typeof(Nullable<>)))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                            dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                        }
                    }
                    DataRow dr = dtReturn.NewRow();
                    foreach (PropertyInfo pi in oProps)
                    {
                        dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                        (rec, null);
                    }
                    dtReturn.Rows.Add(dr);
                }
                return dtReturn;
            }
    
  • 相关阅读:
    常用FPGA功能块记录
    鸿蒙相关
    微波相关
    Python库大全
    C#环境实现代码的自动生成编译
    STM32相关
    硬件相关
    C# 获取枚举中文注释
    C# 获取自定义特性值
    Asp.Net Core 中 Host 与 WebHost的区别
  • 原文地址:https://www.cnblogs.com/volts0302/p/5239345.html
Copyright © 2011-2022 走看看