Null exception being thrown by LINQ to EF

This line of code is throwing an exception:

Dim z = (From r In t Where 
(DateTime.ParseExact(r.appt_date, "yyyyMMdd", CultureInfo.CurrentCulture).Date.Month _
= myDate.Date.Month) And _ 
(DateTime.ParseExact(r.appt_date, "yyyyMMdd", CultureInfo.CurrentCulture).Date.Day = _
 myDate.Date.Day) And _
(DateTime.ParseExact(r.appt_date, "yyyyMMdd", CultureInfo.CurrentCulture).Date.Year = _
myDate.Date.Year)).ToArray

T is defined as the following:

Dim t = (From p In mydb.C_MYTABLE Select p).ToArray

Since someone is going to ask what I'm trying to do... I have appointment dates stored as strings as yyyyMMdd in a DB, i can not change the DB. I need to be able to filter and sort them by date on the app level. Its my understanding that LINQ will not support conversions inside the query?

Jon Skeet
people
quotationmark

(This answer doesn't actually explain your NullReferenceException - but it gives an alternative approach.)

Given that you're checking for an exact date wouldn't it be easiest just to format myDate as yyyyMMdd before the query, and then have:

Dim z = (From r in t Where r.appt_date = formattedDate).ToArray

? That allows you to filter by a specific date - and if you need to filter to a range of dates, I'd expect that you could just use CompareTo. Your dates are in a sortable format, so if you need to find everything between (say) January 10th 2012 and September 20th 2013, you can just say that the string has to be "greater than or equal to" 20120110 and "less than or equal to" 20130920. You can also use this property for sorting - just ordering by the textual representation will be fine.

(That said, if there ever is a database redesign, please urge whoever's responsible to use a more appropriate data type!)

people

See more on this question at Stackoverflow