Reputation: 318
How do you handle a null for a Max value in a linq to entities statement?
int UserLevelID = db.Characters.Where(o => o.UserId == UserID).Max(o => o.LevelID);
Upvotes: 0
Views: 166
Reputation: 1476
You could do the following, replacing 'IFNULLDEFAULTVALUE' with what you want the value to be if o.LevelID is null:
int UserLevelID = db.Characters.Where(o => o.UserId == UserID).Max(o => (o.LevelID != null ? o.LevelID : IFNULLDEFAULTVALUE);
Upvotes: 0
Reputation: 85126
I'm not quite sure what you mean when you say count, since you aren't referring to count anywhere in that code. If you are wonding how to handle a null o.LevelID
you could do something like this:
Max(o => o.LevelID ?? -1);
??
is the coalesce operator in .NET
UPDATE
Try this:
db.Characters.Where(o => o.UserId == UserID).Max(o => o == null ? 0 : o.LevelID);
Upvotes: 1