我试图通过searchstring过滤列表。 It says in the doc on the blue note:
我的过滤如下:
IQueryable<ApplicationUser> customers = from u in _context.Users
where (u.Customer != null && u.IsActive)
select u;
if (!String.IsNullOrEmpty(searchString))
{
customers = customers.Where(s => s.Email.Contains(searchString));
}
但是这个解决方案是区分大小写的,我真的不明白为什么因为我使用IQueryable所以它应该使用默认情况下不区分大小写的数据库提供程序实现?我正在使用EF Core 2,目前只运行本地MSSQLLocalDB
你最好使用LIKE
算子,例如
if (!String.IsNullOrEmpty(searchString))
{
customers = customers.Where(x => EF.Functions.Like(x.Email, $"%{searchString}%"));
}
从EF Core 2.1版开始,您可以使用HasConversion()。但是数据库中的信息将以小写形式存储:
builder.Property(it => it.Email).HasConversion(v => v.ToLowerInvariant(), v => v);
我解决了类似的问题。这一改变解决了我所有的问题。