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
Using Server.Mappath to get a file from a virtual directory
How to prevent the auto update within an Acces 2007 template
"Internet Explorer cannot display the webpage" displays when PC boots
Do you know how I can prevent the Run time error '3010' table 'FA_ledger_report' already exists ? Actually the 'FA_ledger_report' is a Union query.
Wrong NT group names
Import binary data into SQL 2005
Run antivirus/spyware outside Windows XP environment?
Terminal Server 2003 copy and paste files to and from XP Pro clients?
Access 2003: shell command - quest 2
Find a Text in a cell and copy it to another cell.