Issue 50 in linqtoexcel: Nullable types

113 views
Skip to first unread message

linqt...@googlecode.com

unread,
Jul 29, 2011, 8:57:07 AM7/29/11
to linqt...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 50 by daniel.k...@gmail.com: Nullable types
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<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

linqt...@googlecode.com

unread,
Jul 29, 2011, 11:54:07 AM7/29/11
to linqt...@googlegroups.com

Comment #1 on issue 50 by paulyo...@gmail.com: Nullable types
http://code.google.com/p/linqtoexcel/issues/detail?id=50

Try row[15].Cast<double?>();

linqt...@googlecode.com

unread,
Jul 29, 2011, 12:06:21 PM7/29/11
to linqt...@googlegroups.com

Comment #2 on issue 50 by daniel.k...@gmail.com: Nullable types
http://code.google.com/p/linqtoexcel/issues/detail?id=50

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

linqt...@googlecode.com

unread,
Jul 29, 2011, 1:14:32 PM7/29/11
to linqt...@googlegroups.com
Updates:
Status: Accepted

Comment #3 on issue 50 by paulyo...@gmail.com: Nullable types
http://code.google.com/p/linqtoexcel/issues/detail?id=50

(No comment was entered for this change.)

linqt...@googlecode.com

unread,
Feb 14, 2012, 5:48:24 PM2/14/12
to linqt...@googlegroups.com

Comment #4 on issue 50 by paulyo...@gmail.com: Nullable types
http://code.google.com/p/linqtoexcel/issues/detail?id=50

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!

linqt...@googlecode.com

unread,
Nov 1, 2013, 8:16:41 AM11/1/13
to linqt...@googlegroups.com

Comment #5 on issue 50 by peduar...@gmail.com: Nullable types
http://code.google.com/p/linqtoexcel/issues/detail?id=50

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).

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

linqt...@googlecode.com

unread,
Aug 14, 2014, 8:33:06 AM8/14/14
to linqt...@googlegroups.com

Comment #6 on issue 50 by Lai...@gmail.com: Nullable types
http://code.google.com/p/linqtoexcel/issues/detail?id=50

Convert.ToDouble(x["id"].ToString())

linqt...@googlecode.com

unread,
Oct 6, 2014, 12:23:00 AM10/6/14
to linqt...@googlegroups.com

Comment #7 on issue 50 by sure05pa...@gmail.com: Nullable types
https://code.google.com/p/linqtoexcel/issues/detail?id=50

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.. Please find the attachement

Attachments:
groupandsum.PNG 23.9 KB

linqt...@googlecode.com

unread,
Oct 6, 2014, 12:25:23 AM10/6/14
to linqt...@googlegroups.com

Comment #8 on issue 50 by sure05pa...@gmail.com: Nullable types
https://code.google.com/p/linqtoexcel/issues/detail?id=50

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
Reply all
Reply to author
Forward
0 new messages