Reputation: 98
I want to return the id(int) from the database without having to do it this way
var user = context.Users.FirstOrDefault(x => x.Username.Equals(username) && x.Password.Equals(password));
return user.UserID;
Currently I have something like this:
return context.Users.Where(x => x.Username.Equals(username) && x.Password.Equals(password))
.Select(x => x.UserID);
But I receive the following error:
Cannot convert expression type 'System.Linq.IQueryable<int>' to return type 'int'
Is it possible to do so? And if so how do I do it?
Appreciate the help!
Upvotes: 1
Views: 71
Reputation: 1814
context.Users.Find(x=>x.Username.Equals(username) && x.Password.Equals(password)).UserID;
You will need to handle Null Exception.
Upvotes: 0
Reputation: 7800
you must materialize the result:
return context.Users.Where(x => x.Username.Equals(username) &&
x.Password.Equals(password))
.Select(x => x.UserID)
.First(); // or FirstOrDefault(); // or .Single();
Upvotes: 2