EF Core 3.0 translating string.Equals ordinalIgnoreCase correctly

邮差的信 提交于 2020-12-01 11:07:40

问题


Before EF Core 3.0 this worked fine (evaluated on server+client):

var exists = await _context.Countries.AsNoTracking().AnyAsync(x => x.CountryCode.Equals(country.CountryCode, StringComparison.OrdinalIgnoreCase));

What is the best/preferred method to translate the string.Equals(str, StringComparison.OrdinalIgnoreCase)-part now in EF Core 3.0, so that the query evaluates only on the server side.

var exists = await _context.Countries.AsNoTracking().AnyAsync(x => x.CountryCode.ToUpper() == country.CountryCode.ToUpper());

or

var exists = await _context.Countries.AsNoTracking().AnyAsync(x => x.CountryCode.ToLower() == country.CountryCode.ToLower());

or

var exists = await _context.Countries.AsNoTracking().AnyAsync(x => x.CountryCode.ToUpperInvariant() == country.CountryCode.ToUpperInvariant());

or

var exists = await _context.Countries.AsNoTracking().AnyAsync(x => x.CountryCode.ToLowerInvariant() == country.CountryCode.ToLowerInvariant());

or something else?


回答1:


You should not do that, nor use the accepted answer method, you should just use String.Equals() without parameters and configure your database collation, during creation or migration.



来源:https://stackoverflow.com/questions/58640437/ef-core-3-0-translating-string-equals-ordinalignorecase-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!