zoukankan      html  css  js  c++  java
  • 解决DataSet不支持System.nullable

    转自:http://www.cnblogs.com/zoofooo/archive/2012/08/02/2619669.html

    复制代码
     1 using System;
     2 using System.Data;
     3 using System.Collections;
     4 using System.Collections.Generic;
     5 using System.Configuration;
     6 using System.Reflection;
     7 using System.Linq;
     8 using System.Xml.Linq;
     9 
    10 namespace UserFunction
    11 {
    12     /// <summary>
    13     /// Summary description for LinqToDataTable
    14     /// </summary>
    15     static public  class LinqToDataTable
    16     {
    17         static public  DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn)
    18         {
    19 
    20             DataTable dtReturn = new DataTable();
    21 
    22             // column names
    23 
    24             PropertyInfo[] oProps = null;
    25 
    26             // Could add a check to verify that there is an element 0
    27 
    28             foreach (T rec in varlist)
    29             {
    30 
    31                 // Use reflection to get property names, to create table, Only first time, others will follow
    32 
    33                 if (oProps == null)
    34                 {
    35 
    36                     oProps = ((Type)rec.GetType()).GetProperties();
    37 
    38                     foreach (PropertyInfo pi in oProps)
    39                     {
    40               // 当字段类型是Nullable<>时
    41                         Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
    42                         {
    43 
    44                             colType = colType.GetGenericArguments()[0];
    45 
    46                         }
    47 
    48                         dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
    49 
    50                     }
    51 
    52                 }
    53 
    54                 DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
    55                 {
    56 
    57                     dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
    58 
    59                 }
    60 
    61                 dtReturn.Rows.Add(dr);
    62 
    63             }
    64 
    65             return (dtReturn);
    66 
    67         }
    68 
    69         public delegate object[] CreateRowDelegate<T>(T t);
    70     }
    71 }
    72 
    73 /*
    74  * 示例:
    75  * var query = from ....;
    76  * DataTable dt = query.ToDataTable(rec => new object[] { query });
    77  *
    78 */
    复制代码
  • 相关阅读:
    Pyhon数据分析20——matplotlib可视化(二)之柱状图
    程序运行正常,数据库没反应
    Redis在Linux环境下安装的常见错误
    1.1-1.4 sqoop概述及安装cdh版hadoop
    3、css边框以及其他常用样式
    3.15-3.21 hive项目实战
    2、css的存在形式及优先级
    1、css选择器
    3.11-3.14 Hive 企业使用优化2
    3.7-3.10 Hive 企业使用优化1
  • 原文地址:https://www.cnblogs.com/kim-meng/p/12674723.html
Copyright © 2011-2022 走看看