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

前端 未结 4 968
情歌与酒
情歌与酒 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:02

    The error implies that t.TransactionTime is nullable (i.e. DateTime?). Even if the field can't be null in the database if the property is nullable and the query returns no rows FirstOrDefault() will return null as this is the default value of t.TransactionTime

    EDIT

    further to your comments, that is very strange: the below code outputs True

    List dates = new List();
    DateTime date = dates.FirstOrDefault();
    Console.WriteLine(date == DateTime.MinValue);
    

    And i would expect your code to do the same.

提交回复
热议问题