u.Normalized-6ren">
gpt4 book ai didi

c# - 为什么 "The LINQ expression ' x' 无法翻译”?我没有使用 "Where()"

转载 作者:行者123 更新时间:2023-12-04 17:13:13 26 4
gpt4 key购买 nike

当我执行以下代码时,出现错误:

System.InvalidOperationException: The LINQ expression 'DbSet.Where(u => u.NormalizedEmail == __ToLower_0 && u.PasswordHash.SequenceEqual(__pass_1))' could not be translated



但它在 .NET Core 2.2 中运行良好。 .NET Core 3.1 中出现错误。
loginCreds.Password = loginCreds.Password.ToLower();
var pass = Helper.ComputeHash(loginCreds.Password);
var usr = await _context.Users
.FirstOrDefaultAsync(u => u.NormalizedEmail == loginCreds.Email
&& u.PasswordHash.SequenceEqual(pass));

但是,当我替换 u.PasswordHash.SequenceEqual(pass) 时与 u.PasswordHash == new byte[16] (仅用于测试),它有效。所以,问题是 SequenceEqual(byte[] byte)方法。

我该如何解决这个问题?

任何评论将不胜感激。

最佳答案

您可能在这里有答案EF Core 3.0在“受限客户评估”中

For example, if EF Core 2.2 couldn't translate a predicate in a Where() call, it executed an SQL statement without a filter, transferred all the rows from the database, and then filtered them in-memory



....

In EF Core 3.0, we've restricted client evaluation to only happen on the top-level projection (essentially, the last call to Select()). When EF Core 3.0 detects expressions that can't be translated anywhere else in the query, it throws a runtime exception.



使用 EF Core 2.2,查询的部分带有 SequenceEqual实际上并没有在 SQL 中完成。

你应该尝试这样做:
var usr = await _context.Users
.Where(u => u.NormalizedEmail == loginCreds.Email)
.ToListAsync()
.FirstOrDefault(u => u.PasswordHash.SequenceEqual(pass));

关于c# - 为什么 "The LINQ expression ' x' 无法翻译”?我没有使用 "Where()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60554159/

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