开发者

Good way to catch nulls from Entity-Framework?

开发者 https://www.devze.com 2023-02-03 12:29 出处:网络
Right now I\'m doing this, but I don\'t like it very much: decimal maxId = 0d; try { maxId = ent.SaveStates.Max(c => c.Id);

Right now I'm doing this, but I don't like it very much:

decimal maxId = 0d; 
try
{
  maxId = ent.SaveStates.Max(c => c.Id);
}
catch (Exception ex) //no ent开发者_如何学运维ries in the db
{
  maxId = 1; 
}

Is there a better way to handle nulls from the DB with entity-framework?


Here's this:

maxId = ent.SaveStates.Count() > 0 ? ent.SaveStates.Max(c => c.Id) : null;

I don't think that will cause two queries, but I'd profile it to make sure.

0

精彩评论

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