EF Core 3.0 中的 LINQ 重大更改。如何在没有收到警告 CA1308 的情况下比较字符串?

xav*_*ier 5 .net-core .net-core-3.0 ef-core-3.0 entity-framework-core-3.0

我有以下代码,它在 EF Core 2.1 上运行良好:

.FirstOrDefault(a => (a.Name.Equals(b, StringComparison.InvariantCultureIgnoreCase).

(好吧,运行良好意味着我得到了正确的结果,即使它正在客户端进行评估而我不知道)。

我升级到 EF Core 3.0 并且我没有收到任何错误,但是这段代码没有给出预期的结果。

我在这里看到一个解决方案。我试过了,a.Name.ToLower() == b.ToLower()但后来我得到了错误

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)'

如果我使用 aToLower(CultureInfo.InvariantCulture)我收到消息:

Error CA1308 In method 'MyFunction', replace the call to 'ToLower' with 'ToUpperInvariant'.

如果我使用ToUpperInvariant(),则会收到错误消息(我已经知道EF Core 3.0中的LINQ 重大更改):

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 又在数据库中而不是在客户端运行查询?

xav*_*ier 5

正如 PanagiotisKanavos 评论的那样,解决方案是简单地使用a.Name == b. 简单又有效!

  • 最好在这个答案中包含它取决于数据库/列的排序规则。例如,如果您使用 SQL_Latin1_General_CP1_CS_AS,则此解决方案将不起作用! (4认同)