Linq to DataSet - 处理空值 [英] Linq to DataSet - Handling Null Values

查看:167
本文介绍了Linq to DataSet - 处理空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将LINQ转换为DataTable。



我从StackOverflow中偷走了以下扩展方法:

  public static DataTable ToDataTable< T>(此IEnumerable< T>项目)
{
var tb = new DataTable(typeof(T).Name);
PropertyInfo [] props =
typeof(T).GetProperties(BindingFlags.Public
| BindingFlags.Instance);

foreach(在道具中的var prop)
{
tb.Columns.Add(prop.Name,prop.PropertyType);
}

foreach(项目中的项目)
{
var values = new object [props.Length];
for(var i = 0; i< props.Length; i ++)
{
values [i] = props [i] .GetValue(item,null);
}

tb.Rows.Add(values);
}
return tb;
}

当表包含空值时,它会抛出异常。
(即)

  DataSet不支持System.Nullable<> 

Comission(十进制类型)列包含空值)



  tb.Columns.Add(prop.Name,prop.PropertyType) ; 

如何解决?

解决方案

这是一个pimped版本:

  public static DataTable ToDataTable< T>(this IEnumerable& ; items){
DataTable table = new DataTable(typeof(T).Name);
PropertyInfo [] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach(道具中的var prop){
类型propType = prop.PropertyType;

//是否为可空类型?获取底层类型
if(propType.IsGenericType&& propType.GetGenericTypeDefinition()。Equals(typeof(Nullable)))
propType = new NullableConverter(propType).UnderlyingType;

table.Columns.Add(prop.Name,propType);
}

foreach(项目中的项目){
var values = new object [props.Length];
for(var i = 0; i< props.Length; i ++)
values [i] = props [i] .GetValue(item,null);

table.Rows.Add(values);
}

返回表;
}

修改:修改了我的代码,它,它的作品! :)


I have a requirement to convert LINQ to DataTable.

I stole the following Extension Method from StackOverflow:

public static DataTable ToDataTable<T>(this IEnumerable<T> items)
        {
            var tb = new DataTable(typeof(T).Name);
            PropertyInfo[] props = 
            typeof(T).GetProperties(BindingFlags.Public
                                  | BindingFlags.Instance);

            foreach (var prop in props)
            {
                tb.Columns.Add(prop.Name, prop.PropertyType);
            }

            foreach (var item in items)
            {
                var values = new object[props.Length];
                for (var i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }
            return tb;
  } 

When the table contains null values it throws exception. (i.e)

DataSet does not support System.Nullable<> 

Comission (Decimal type) column contains null value)

at

tb.Columns.Add(prop.Name, prop.PropertyType);

How to fix it?

解决方案

Here's a pimped version:

public static DataTable ToDataTable<T>(this IEnumerable<T> items) { 
    DataTable table = new DataTable(typeof(T).Name);
    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 

    foreach (var prop in props) {
        Type propType = prop.PropertyType;

        // Is it a nullable type? Get the underlying type
        if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            propType = new NullableConverter(propType).UnderlyingType; 

        table.Columns.Add(prop.Name, propType);
    } 

    foreach (var item in items) { 
        var values = new object[props.Length]; 
        for (var i = 0; i < props.Length; i++)
            values[i] = props[i].GetValue(item, null);  

        table.Rows.Add(values); 
    }

    return table;
}

Edit: Modified my code a bit, tested it, it works! :)

这篇关于Linq to DataSet - 处理空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆