传递谓词时实体框架返回不同的结果
string personName= "JoHn";
//(in my table iam already having a person named='john')
Func<Person, bool> predicate = (p) => p.Name== personName;
var res2 = dataContext.Persons.Any(predicate); //returns false
var res1 = dataContext.Persons.Any(p=>p.Name== personName); // returns true
我认为使用谓词它会考虑 personName
属性的 case
,而没有它它只是忽略 case
。
有谁知道为什么吗??
string personName= "JoHn";
//(in my table iam already having a person named='john')
Func<Person, bool> predicate = (p) => p.Name== personName;
var res2 = dataContext.Persons.Any(predicate); //returns false
var res1 = dataContext.Persons.Any(p=>p.Name== personName); // returns true
I think with predicate its considering case
of personName
property while without it its just ignoring case
.
Any one know why??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Func
是一个委托,这意味着您正在 LINQ-to-Objects 中(即在内存中,在 C# 中)运行它。 .NET 字符串区分大小写,因此这将应用大小写。然而,这个版本:
使用
IQueryable
和表达式树;它将作为 TSQL 过滤器执行,它将应用数据库规则。这里发生的情况取决于数据库的配置方式(可以区分大小写或不区分大小写,具体取决于数据库)。如果您希望它们都使用相同的逻辑,请注意此处的区别:
添加
Expression<...>
使其成为表达式树,而不是委托,因此它是“组合”并在数据库中执行的(通过 TSQL 翻译),与以下内容完全相同:A
Func<Page, bool>
is a delegate, which means you are running this in LINQ-to-Objects (i.e. in memory, in C#). .NET strings are case sensitive, so this will apply case.This version, however:
is using
IQueryable<T>
and expression-trees; it will be executed as a TSQL filter, which will apply database rules. What happens here depends on how your DB is configured (it could be either case-sensitive or case-insensitive, depending on the database).If you want them both to use the same logic, then note the difference here:
The addition of
Expression<...>
makes this an expression-tree, not a delegate, so it is "composed" and executed at the database (via TSQL translation), exactly the same as: