C# linq issue
-
I have a C# 2010 application that I would like to add the following lines of code:
eDataContext rData = new eDataContext();
DateTime complete_date = (DateTime)(from a in rData.cust
where a.PkID == pkId
select a.Complete_Date).FirstOrDefault();
if (complete_date != null)The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?
-
I have a C# 2010 application that I would like to add the following lines of code:
eDataContext rData = new eDataContext();
DateTime complete_date = (DateTime)(from a in rData.cust
where a.PkID == pkId
select a.Complete_Date).FirstOrDefault();
if (complete_date != null)The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?
DateTime is not nullable. If you need to have a null date option, use DateTime? to declare it inside. This tells the compiler to create it as nullable.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
I have a C# 2010 application that I would like to add the following lines of code:
eDataContext rData = new eDataContext();
DateTime complete_date = (DateTime)(from a in rData.cust
where a.PkID == pkId
select a.Complete_Date).FirstOrDefault();
if (complete_date != null)The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?
FirstOrDefault
is your clue. If not found, it will return the default value for DateTime which isDateTime.MinValue
. Use:if (complete_date != DateTime.MinValue)
Or
if (complete_date != default(DateTime))
instead. null is default for reference types such as classes and interfaces, for value types such as structs there is normally a static readonly field or property available such as
MinValue
orEmpty
, or you can use the parameterless constructor to get the default, ordefault(...)
as above.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I have a C# 2010 application that I would like to add the following lines of code:
eDataContext rData = new eDataContext();
DateTime complete_date = (DateTime)(from a in rData.cust
where a.PkID == pkId
select a.Complete_Date).FirstOrDefault();
if (complete_date != null)The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?
DateTime is a ValueKind, so comparing it with null isn't very useful (it'll always not be null). If your LINQ query doesn't find any rows, then FirstOrDefault() will return a default instance of the requested data type (DateTime, in this case). There's a clue in the name ;) If you want to know if you found any rows or not, either test the query first (e.g. Any()) before evaluating First(), or compare the returned value with the default DateTime value (DateTime.MinValue, IIRC). If you do that however, you won't be able to tell the difference between no rows, and rows where the first had the minimum datetime value.