Why this code works?
Does NHibernate employ object-interning?
If not, the following works because NHibernate overload Equals operator?
foreach (var c in s.Query<Country>())
{
Console.WriteLine("\n{0}", c.CountryName);
// code in question
foreach(var p in s.Query<Person>().Where(x => x.Country == c) )
Console.WriteLine("{0}", p.PersonName);
}
Can’t tell for NHibernate for sure, but if it follows the same rules as Hibernate (as I suspect), it guarantees that in a given session, you may only have one instance of a given entity for a given ID. This means that if a Country instance is already loaded in the session and if a person’s country has the ID of this loaded instance, the country and the person’s country will be the same instance, which is quite logical.
The session is a cache, and there is only one instance of an entity with a given ID in this cache. No need for operator overloading.