开发者

Linq to Entities, getting Max date when there is no data

开发者 https://www.devze.com 2023-04-10 01:43 出处:网络
Here\'s my statement: startDate = (from n in db.Nodes where n.SeedID == mySeedID select n.CreatedDate).Max<DateTime>();

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?>();
0

精彩评论

暂无评论...
验证码 换一张
取 消