我有以下类:
public class Employee
{
public string Name {get; set;}
...
}
在 EF Core 2.1 中,使用 LINQ 查询
Employee GetEmployeeByName(string name) {
return Context.Employee.Where ( w =>String.Compare(w.Name, name, true) == 0).FirstOrDefault();
}
将其转换为Net Core EF 3.1后,出现错误。
LINQ表达式无法翻译。请将查询重写为可翻译的形式,或通过插入对
AsEnumerable()
、AsAsyncEnumerable()
、ToList()
或ToListAsync()
之一的调用来显式切换到客户端评估。
我必须更改查询为
Employee GetEmployeeByName(string name) {
return Context.Employee.Where ( w =>w.Name.ToLower() == name.ToLower()).FirstOrDefault();
}
有更好的方法吗?
.Where(x => x.Name == "Smith")
,大小写敏感取决于数据库,而SQL Server默认情况下通常不区分大小写。 - Caius Jard