gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:59:39 26 4
gpt4 key购买 nike

有人可以解释一下为什么我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com