I am trying to select MAX LevelId value for a user and then assign that value to a variable.
In plain SQL the equivalent is:
SELECT MAX(LevelID) AS MaxLevel FROM Character WHERE (UserId = 'John') //Check User existing Characters max level per user
int UserLevelID = 0;
var userQuery = (from o in db.Characters where o.UserId == UserID select o);
UserLevelID = userQuery.Max(LevelID);
Here lies the problem. I don't know how to extract the max value of levelID
You can do it all in one query:
var userLevelId = db.Characters.Where(o => o.UserID == UserID) .Max(o => o.LevelID);
I've changed it away from being a query expression because that was introducing more cruft - use query expressions where they actually help readability, and extension method calls for simple solutions where they're more readable.
var theLevel = db.Characters.Where(z => z.UserId == UserID).Max(z => z.LevelID);