troubleshooting Question

Linq MAX returns null on DateTime Type

Avatar of johngrant_ks
johngrant_ks asked on
.NET ProgrammingASP.NETC#
21 Comments1 Solution6250 ViewsLast Modified:
Hi

System.Nullable<DateTime> DateToSelect = (from p in db.Archived select p.TestDate).Max();

I'm trign to return a date from a table but if there are no records in the table i get the Error

The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type.

How do i stop this null error if there are no records matching the criteria

I also have the problem when there are records in the table and i place a where statement in the LINQ i get the same error even if there are records that meet the criteria.

e.g.
System.Nullable<DateTime> WinningDateToSelect = (from p in db.qtxt_Competitions_Archived_Winners where p.ID = 1 select p.WinningDate).Max();
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 21 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 21 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros