我已阅读Case insensitive Contains(string),Why would Entity Framework not be able to use ToString() in a LINQ statement?以及其他许多问题,但我仍然无法找到正确的方法。
string searchString = "max";
private db = new DbContext();
IQueryable<Person> persons = from m in db.Persons
select m;
if (null != persons)
{
//do what it should but marked as codesmell because of .ToStrong() with no culture
persons = persons.Where(s =>
s.person_name.ToLower().Contains(searchString.ToLower()));
//NotSupportedException
persons = persons.Where(s =>
cultureInfo.CompareInfo.IndexOf(s.person_name, searchString, CompareOptions.IgnoreCase) >= 0);
//NotSupportedException
persons = persons.Where(s =>
s.person_name.ToLower(CultureInfo.InvariantCulture).Contains(searchString.ToLower(CultureInfo.InvariantCulture)));
}
在使用LINQ的数据库中进行不区分大小写的字符串搜索的正确方法是什么?