c# - Linq To Entities 异常不识别方法,无法翻译成存储表达式

标签 c# linq-to-entities

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

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 查询。

例子

var hashedPassword = GenerateHashWithSalt(password, GetUserID(username));
var user = (from p in Users
        where p.Password == hashedPassword
        select p).FirstOrDefault();

关于c# - Linq To Entities 异常不识别方法,无法翻译成存储表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676047/

相关文章:

c# - EntityFramework LINQ 查询计数失败但查询返回结果。如何优化 LINQ 查询?

c# - 如何将 Linq.ParallelQuery 转换为 Linq.IQueryable

c# - 单元测试 AAA 模式中的数据提取

c# - 如何限制excel采用默认时间格式

c# - 我可以像使用 LINQ to SQL 一样使用 Entity Framework 吗?

c# - 为什么 IQueryable 上的 .Where() 根据是否将 Lamba 或 Func<T,Tresult> 作为参数传递返回不同的类型

.net - 将 DataContext 对象作为 'ref' 参数传递有什么缺点吗?

c# - 翻译 ninject ISecureDataFormat 绑定(bind)到 Autofac

c# - 为什么 EF 核心中的 [Timestamp] 列由 byte[] 列而不是 UInt64 支持

c# - 如何在对象的表达式中分配 <T> 的表达式?