Linq to Entities, getting Max date when there is no data
Here's my statement:
startDate = (from n in db.Nodes
where n.SeedID == mySeedID select n.CreatedDate).Max<DateTime>();
It works fine when there is data for that SeedID. However, some times the seed is new, so there are no nodes. The statement results in an InvalidOperationException. I could wrap this in tr开发者_运维知识库y/catch. Is there a better way to deal with this scenario?
Maybe use a nullable DateTime?
startDate = (from n in db.Nodes
where n.SeedID == mySeedID
select (DateTime?)n.CreatedDate).Max<DateTime?>();
精彩评论