Jimsan
Jimsan

Reputation: 305

ASP.Net - Issue with DateTime in Linq - VB

I am using Linq to SQL to get some data so I can highlight a day on a calendar control where a record exists based on the date that record was added to the database.

There is a field in my database (datestamp) of data type date.

I am declaring an IEnumerable to achieve this as below:

Private unapprovedFireDays As IEnumerable(Of DateTime) = Nothing

My code in the page_prerender event to get the data can be seen below:

If calFire.VisibleDate.Year <> 1 Then
        baseDate = calFire.VisibleDate
    Else
        baseDate = DateTime.Now
    End If

    startDate = New DateTime(baseDate.Year, baseDate.Month, 1)
    endDate = startDate.AddMonths(1).AddDays(-1)


    Dim fdc As New FireAlarmDataContext()


    unapprovedFireDays = (From f In fdc.FireAlarms _
                          Where f.DateStamp <= endDate And _
                          f.DateStamp >= startDate And _
                          f.SupervisorChecked = "0" _
                          Order By f.DateStamp _
                          Select New DateTime(f.DateStamp.year, _
                                              f.DateStamp.month, _
                                              f.DateStamp.day) Distinct).ToArray()

For some reason, i'm getting blue lines under the f.datestamp.year, f.datestamp.month and f.datestamp.day items of code. If I hover over this it says 'Year' is not a member of 'Date?'

I have virtually identical code working with another table using a field in that table called datestamp of data type date and that works fine.

I am unsure why it thinks year should be a member of Date? and not Date. Have I missed something obvious?

Upvotes: 2

Views: 647

Answers (1)

debater
debater

Reputation: 476

I suspect the problem is quite simple.

'Year' is not a member of 'Date?' (a nullable type), it is a member of 'Date'.

I wonder if putting 'f.DateStamp.Value.Year' instead of 'f.DateStamp.year' would solve the problem?

Upvotes: 3

Related Questions