Goal:
- order records by ‘start_date’
- select first record
- get the id of the record returned
When I attempt to order all events by ‘start_date’ and select the first record returned …
code examples:
var aa = (from a in db.Events
where a.start_date >= System.DateTime.Now.Date
orderby a.start_date ascending
select a).FirstOrDefault();
Response.Write(aa.id.ToString());
…I throw an “Object reference not set to an instance of an object.” error.
FirstOrDefault()returns null if yourIQuerablereturns an empty collection – that’s why you get that exception. Looks like there is no event that has a start date larger thanDateTime.Now.Date