简体   繁体   English

NHibernate Linq中的Equals方法和==运算符有什么区别?

[英]What's the Differences between Equals method and == operator in NHibernate Linq?

When I used the following code: 当我使用以下代码时:

session.Query<Url>().Where(ur => ur.QueryString == url.QueryString)

I found my results, but when I tried the same with .Equals, didn't. 我找到了结果,但是当我使用.Equals尝试相同时,结果却没有。 Why? 为什么? The Equals() method is the default one, no overrides. Equals()方法是默认方法,没有覆盖。

What's the difference between these LINQ queries? 这些LINQ查询之间有什么区别?

The biggest difference is that they are different "functions". 最大的区别是它们是不同的“功能”。 public static bool operator == (Object other) and public bool Equals(Object other) can be independently defined. public static bool operator == (Object other)public bool Equals(Object other)可以独立定义。

However, even when they do the same thing (or one calls the other), the fact that they are different calls still means something to a Linq provider. 但是,即使它们执行相同的操作(或一个调用另一个),但对于Linq提供程序来说,它们是不同的调用仍然意味着某些事情。 The upshot is that the NHibernate queryable provider, which "translates" the expression tree you create using a Linq query or method chain, might treat the two differently. 结果是NHibernate可查询提供程序可能会区别对待两者,后者可“翻译”使用Linq查询或方法链创建的表达式树。 It might correctly parse .Where(ur => ur.QueryString == url.QueryString) into WHERE ur.QueryString = 'MyUrlQueryStringValue' , but .Where(ur => ur.QueryString.Equals(url.QueryString)) might be ignored as unusable, or return an error, or be parsed into WHERE 0 . 它可以正确地将.Where(ur => ur.QueryString == url.QueryString)解析为WHERE ur.QueryString = 'MyUrlQueryStringValue' ,但是.Where(ur => ur.QueryString.Equals(url.QueryString))可能会被忽略作为不可用,或者返回错误,或者被解析为WHERE 0

One would end up using 'Equals' when comparing objects...or entities. 在比较对象或实体时,最终将使用“等于”。

'==' is something you will always use when comparing just values...such as strings. 当仅比较值(例如字符串)时,将始终使用“ ==”。

==比较引用,而Equals()是一个虚拟方法,可以实现,但是可以实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM