EF Core 3.0 中的 LINQ 重大更改。如何在不收到警告 CA1308 的情况下比较字符串?
LINQ Breaking changes in EF Core 3.0. How can I compare strings without getting the warning CA1308?
我有以下代码,运行与 EF Core 2.1 结合得很好:
.FirstOrDefault(a => (a.Name.Equals(b, StringComparison.InvariantCultureIgnoreCase)
.
(好吧,运行宁好意味着我得到了正确的结果,即使它是在客户端评估的,我不知道)。
我更新到 EF Core 3.0 并且没有收到任何错误,但是这段代码没有给出预期的结果。
我看到了here a solution. I tried a.Name.ToLower() == b.ToLower()
but then I got the the error:
Error CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'MyFunction(string, string)' with a call to 'string.ToLower(CultureInfo)'
如果我使用 ToLower(CultureInfo.InvariantCulture)
我会收到消息:
Error CA1308 In method 'MyFunction', replace the call to 'ToLower' with 'ToUpperInvariant'.
如果我使用 ToUpperInvariant()
,则会出现错误(我已经知道 EF Core 3.0 中的 LINQ breaking changes):
The LINQ expression (... all the expression...) could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
那么,我就是起点
有没有办法既符合 CA1304 又符合 运行 数据库中而不是客户端中的查询?
正如 PanagiotisKanavos 评论的那样,解决方案是简单地使用 a.Name == b
。简单易用!
我有以下代码,运行与 EF Core 2.1 结合得很好:
.FirstOrDefault(a => (a.Name.Equals(b, StringComparison.InvariantCultureIgnoreCase)
.
(好吧,运行宁好意味着我得到了正确的结果,即使它是在客户端评估的,我不知道)。
我更新到 EF Core 3.0 并且没有收到任何错误,但是这段代码没有给出预期的结果。
我看到了here a solution. I tried a.Name.ToLower() == b.ToLower()
but then I got the the error:
Error CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'MyFunction(string, string)' with a call to 'string.ToLower(CultureInfo)'
如果我使用 ToLower(CultureInfo.InvariantCulture)
我会收到消息:
Error CA1308 In method 'MyFunction', replace the call to 'ToLower' with 'ToUpperInvariant'.
如果我使用 ToUpperInvariant()
,则会出现错误(我已经知道 EF Core 3.0 中的 LINQ breaking changes):
The LINQ expression (... all the expression...) could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
那么,我就是起点
有没有办法既符合 CA1304 又符合 运行 数据库中而不是客户端中的查询?
正如 PanagiotisKanavos 评论的那样,解决方案是简单地使用 a.Name == b
。简单易用!