emmanuelkhumbudzo / linqtoexcel

Automatically exported from code.google.com/p/linqtoexcel
0 stars 0 forks source link

Nullable types #50

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
How can I return null from an empty cell instead of default value?

I use 
row[15].Cast<double>();

I see this in your code

       public T Cast<T>()
        {
            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

Original issue reported on code.google.com by daniel.k...@gmail.com on 29 Jul 2011 at 12:55

GoogleCodeExporter commented 9 years ago
Try row[15].Cast<double?>();

Original comment by paulyo...@gmail.com on 29 Jul 2011 at 3:53

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

Original comment by daniel.k...@gmail.com on 29 Jul 2011 at 4:05

GoogleCodeExporter commented 9 years ago

Original comment by paulyo...@gmail.com on 29 Jul 2011 at 5:14

GoogleCodeExporter commented 9 years ago
Hi Daniel,

I'm trying to reproduce this issue but cannot.

cell.Cast<double>() returns 0, which you don't want.

cell.Cast<double?>() return null because null is double? default's value.

Can you try cell.Cast<double?>() (with the question mark) and see if it returns 
null for you.

Thanks for helping me out!

Original comment by paulyo...@gmail.com on 14 Feb 2012 at 10:48

GoogleCodeExporter commented 9 years ago
Invalid cast from 'System.Double' to 'System.Nullable`1[[System.Double, 
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

This is the error message trying to cast to 'double?' (i.e. nullable double).

Original comment by peduar...@gmail.com on 1 Nov 2013 at 12:16

GoogleCodeExporter commented 9 years ago
Convert.ToDouble(x["id"].ToString())

Original comment by Lai...@gmail.com on 14 Aug 2014 at 12:33

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hi,
i had same issue as above and i tried above code and it worked as expected.
Could you please help me to solve below issue where i want to groupby and sum 
value.

int sum = 0;
var groupp=from k in excelFile.Worksheet(sheetName).ToList() select k;
var grouped = groupp.GroupBy(row => row["Issue Type"]);
foreach (var g in grouped)
{
sum = g.Sum(t => Convert.ToInt32(t["Time Spent"]));
MessageBox.Show("key=" + g.Key + "sum=" + sum);
}

and it is displaying same value as it is in Excel column. its not displaying 
Summed value and not grouping.. Find the attached screenshot

Original comment by sure05pa...@gmail.com on 6 Oct 2014 at 4:25

Attachments: