Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to DataSet - Handling Null 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?

like image 469
Dhina Avatar asked Jan 30 '10 19:01

Dhina


People also ask

How can you handle null values in database?

By using the assignment operator (“=”), you can set any value of a column to NULL by using the Update Statement.

Can LINQ return null?

in conclusion no, it won't return null since null can't say sequence contains no elements it will always say object reference not set to an instance of an object ;) Oh, your explanation helps further understanding.

Is null SQL in LINQ?

NULL in SQL means, "value absent, will match any comparison", whereas null in . NET means "no object, comparing against null will always yield false". Save this answer.

How do you handle database null values in C#?

IsNullOrEmpty() Method of C# This method is used when you want to check whether the given string is Empty or have a null value or not? If any string is not assigned any value, then it will have Null value. The symbol of assigning Null value is “ “or String. Empty(A constant for empty strings).


1 Answers

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! :)

like image 134
Zyphrax Avatar answered Nov 15 '22 12:11

Zyphrax