Get default value type (DateTime) in Linq to Sql query with empty results

前端 未结 4 951
情歌与酒
情歌与酒 2021-01-27 03:07

I am having a problem returning a default DateTime value from a complex Linq-to-Sql query.

Hopefully the following simplified example shows the

4条回答
  •  甜味超标
    2021-01-27 04:04

    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.

提交回复
热议问题