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

前端 未结 4 949
情歌与酒
情歌与酒 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 03:47

    users.Select(u =>
      new MyDomainObject(
         u.Id,
         u.Transactions
            .Where(t => false) // empty results set
            .Select(t => t.TransactionTime)
            .Any() ?
         u.Transactions
            .Where(t => false) // empty results set
            .Select(t => t.TransactionTime) // TransactionTime is DATETIME NOT NULL
            .OrderByDescending(x => x)
            .FirstOrDefault() : // I want DateTime.MinValue (or SqlDateTime.MinValue)
         DateTime.MinValue
      )
    );
    

    This will supply DateTime.MinValue if there are no transactions available for that object.

提交回复
热议问题