I am having a problem returning a default DateTime
value from a complex Linq-to-Sql
query.
Hopefully the following simplified example shows the
I'd be very tempted to actually use a nullable DateTime
for this. For example, from your "Car" sample:
var test = _dataContext.GetTable
.Select(c =>
c.MechanicVisits
.Select(m => m.ServiceRecord)
.Select(s => (DateTime?) s.ServiceDate)
.OrderByDescending(d => d)
.FirstOrDefault()
).ToList();
That way I suspect you'll end up with it working and giving you null DateTime?
values. You could always transform that later if you wanted:
var test = _dataContext.GetTable
.Select(c =>
c.MechanicVisits
.Select(m => m.ServiceRecord)
.Select(s => (DateTime?) s.ServiceDate)
.OrderByDescending(d => d)
.FirstOrDefault()
).AsEnumerable()
.Select(dt => dt ?? DateTime.MinValue)
.ToList();
Original answer (doesn't work)
Hmm. I won't claim to fully understand the reasons for this, but here's a potential workaround:
users.Select(u =>
new MyDomainObject(
u.Id,
u.Transactions
.Where(t => false)
.Select(t => t.TransactionTime)
.OrderByDescending(x => x)
.DefaultIfEmpty(DateTime.MinValue)
.First()
)
);
In other words, if the result set is empty, use the specified default - and then take the first result of the now-definitely-not-empty sequence.