0

I am new to LINQ. I am trying to convert an existing MySQL query. I have a table of events and a table that builds "session" records.

The event table has many, mostly varchar, columns and at least one datetime column.

The session table has a startId field that is a link back to the primary key of the event table and specifies the event that started the session. There is an endId field that will either be NULL (session is active) or be a pointer into the event table to the record that ended the session.

I want to search for sessions that were active on a certain date. in MySQL I can say:

select
    *
from
    Sessions s
    inner join EventLogs eStart on s.StartId equals eStart.Id
    inner join EventLogs eEnd on s.EndId equals eEnd.Id
where
    '<some date string>' betwen eStart.Date and ifnull(eEnd.Date, now())

I have not seen that LINQ has a between operator so I do the next best thing. What is getting me at this instant is that I am not sure how to express the ifnull(eEnd.Date, now()) in LINQ. I am currently trying:

var result = 
    from s in dbContext.Sessions
    join eStart in dbContext.EventLogs on s.StartId equals eStart.Id
    join eEnd in dbContext.EventLogs on s.EndId equals eEnd.Id
    where ('<some date string>' >= eStart.Date) 
        && ('<some date string>' <= (eEnd.Date ?? DateTime.Today))

But I am told that the ?? operator can not be applied to operands of type DateTime. What is the magic here?

7 Reeds
  • 2,419
  • 3
  • 32
  • 64
  • 2
    You could use ?? if your DateTime is declared as nullable (DateTime?). – Jacob Sep 21 '16 at 17:01
  • 2
    You make make the DateTime nullable if you need to accommodate null values in the database. `DateTime? startdate`http://stackoverflow.com/questions/221732/datetime-null-value – Matthew Alltop Sep 21 '16 at 17:01

0 Answers0