paulyoder/LinqToExcel — Issue #6: Return null from an empty cell instead of default value?
Author: paulyoder
Created: 2011-12-12 14:25:58+00:00
State: open

http://code.google.com/p/linqtoexcel/issues/detail?id=50

How can I return null from an empty cell instead of default value?

I use
row[15].Cast();

I see this in your code

public T Cast()
{
return (Value == null || Value is DBNull) ?
default(T) :
(T)Convert.ChangeType(Value, typeof(T));
}

but also this in extensions

public static object Cast(this object @object, Type castType)
{
//return null for DBNull values
if (@object.GetType() == typeof(DBNull))
return null;

//checking for nullable types
if (castType.IsGenericType &&
castType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
castType = Nullable.GetUnderlyingType(castType);
}
return Convert.ChangeType(@object, castType);
}

thx

I have tried this first but cell.Cast has check for null value and doesn't care if type is nullable