Hello,
I am converting a small ASP.Net WebAPI application into a ServiceStack app.
At the time of dealing with a query that uses DateTimeOffset I am getting error:
System.NullReferenceException:
“Object reference not set to an instance of an object”
Thrown by:
public NCNNumber LastEntryAt(DateTimeOffset date)
{
var q = Db.From<NCNNumber>()
.Where(e => e.CreatedAt.Year == date.Year)
.Where(e => e.CreatedAt.Month == date.Month)
.Where(e => e.CreatedAt.Day == date.Day)
.OrderByDescending(e => e.Sequence);
return Db.Single(q);
}
So far I had to use the following code which works, but I would like to understand why the code above did not work.
This is working: (I had to remove dollar sign of interpolated strings, don’t know how to escape them in here):
public NCNNumber LastEntryAt(DateTimeOffset date)
{
var q = Db.From<NCNNumber>();
q.Where("(DATEPART(year, {q.Column<NCNNumber>(n => n.CreatedAt)})) = {date.Year}");
q.Where("(DATEPART(month, {q.Column<NCNNumber>(n => n.CreatedAt)})) = {date.Month}");
q.Where("(DATEPART(day, {q.Column<NCNNumber>(n => n.CreatedAt)})) = {date.Day}");
q.OrderByDescending(n => n.Sequence);
return Db.Single(q);
}