I’m writing a piece of code with linq which has to hash a string (in that case my login) and then try to find it into my database.
I tried :
var userFind = context.Users.FirstOrDefault(user =>HashHelper.HashCode(user.Login).Equals(u.Login));
I got an error because of the HashCode.
I wouldn’t read all of my list with a foreach. I’m wondering if it’s possible to do so with one line of code.
Regards.
Edit: I found a way to do so, but it isn’t as lighter as I expected.
User userFind = null;
foreach (var user in context.Users)
{
string hashedLogin = HashHelper.HashCode(user.Login);
if(hashedLogin.Equals(u.Login))
{
userFind = user;
}
}
If you don’t have too many users, you can do it on one line like this.
The important bit is the
ToList()which evaluates the EF part and makes the rest linq-to-objects. This means the comparison will be done on the client and all users will be retrieved from the server. It is equivalent to your edit.If performance is a problem you should store the hashcode in the database too.