Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq To Entities exception does not recognize the method and cannot be translated into store expression

Can somebody please give an explanation to why I am getting this error with my LINQ code?

LINQ to Entities does not recognize the method 'System.String GenerateHashWithSalt(System.String, System.String)'method and this method cannot be translated into a store expression.

var query = (from u in context.Users
                         where 
                         u.Password ==  
                          GenerateHashWithSalt(password, GetUserID(username))
                         select u).Count();
like image 717
Nivash Avatar asked Jul 13 '11 08:07

Nivash


1 Answers

You are trying to pass a method to EF which tries to convert that method to known SQL command. SQL doesn't know about GenerateHashWithSalt(System.String, System.String)
You should first assign the result to a variable then generate your Linq to Entity Query.

Example

var hashedPassword = GenerateHashWithSalt(password, GetUserID(username));
var user = (from p in Users
        where p.Password == hashedPassword
        select p).FirstOrDefault();
like image 156
Jethro Avatar answered Oct 13 '22 00:10

Jethro