Microsoft
Software
Hardware
Network
Question : linq query handling null datetime
I have a dataset with two tables. I have a linq query that is joining the tables and returning data as expected unless EndDate is null. If the date field is null I get an error
"Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type."
I need all rows even if EndDate is null. What is the best way to handle the null issue and get all rows of data?
var query = from xxxxx
where xxxxx
select new ReturnResult
{
LastName = person.Field
("Last
Name"),
FirstName = person.Field
("Firs
tName"),
StartDate = person.Field
("St
artDate"),
EndDate = person.Field
("En
dDate")
}
ReturnResult is a class I defined.
public class PersonnelResult
{
public String LastName
{get; set;}
public String FirstName
{get; set;}
public DateTime StartDate
{get; set;}
public DateTime EndDate
{get; set;}
}
Answer : linq query handling null datetime
make your date time variables
DateTime? yourDate; (this is a nullabe datetime object)
Random Solutions
Error importing .reg file
split comma delimited values into seperate columns
Access 2007 converts all my projects to .mde when I save to local drive
DoCmd.RunMacro Problem/Question
Domain controller for Group Policy operations is not available. and a couple other things.
Cannot create Web app for Shared Service Provider setup in Sharepoint 2007
Exchange Server 2003 SMTP Problems
Requery needed when adding records, but not when Editing - causes problem with Continuous form used to select records
Function Key to go to the bottom of and edit field
How to type a character string using a shortcut key in VBA.