开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜