Linq To Entities异常无法识别该方法,因此无法转换为商店表达式 [英] Linq To Entities exception does not recognize the method and cannot be translated into store expression

查看:112
本文介绍了Linq To Entities异常无法识别该方法,因此无法转换为商店表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下为什么我的LINQ代码出现此错误吗?

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

LINQ to Entities无法识别方法'System.String GenerateHashWithSalt(System.String,System.String)'方法和这个 方法不能转换为商店表达式.

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();

推荐答案

您正试图将方法传递给EF,EF会尝试将该方法转换为已知的SQL命令. SQL不了解GenerateHashWithSalt(System.String, System.String)
您应该首先将结果分配给变量,然后生成您的Linq to Entity Query.

例子

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();

这篇关于Linq To Entities异常无法识别该方法,因此无法转换为商店表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆