Today's whim, and suddenly want to understand why LINQ in the multi-conditional join, the use of the new anonymous class can be successful, this is why? Why is LINQ using equals instead of the common "= ="?
Subsequently, the following tests were performed, and it was found that equals did not handle the anonymous class object and the named class object in a consistent way
To learn more before discovering that
In C #, the anonymous class is rewritten so that the conditions in the first LINQ can be judged successfully. However, according to the test, to make two anonymous objects equal, attribute properties, property names, and property values must be identical.
Learn more about why equals behaves differently to named class objects than anonymous class objects, discovering that equals is only a virtual method of object, that the custom class in the test does not override equals, and then calls the objects method and returns false. At the same time, can also refer to this friend's article to learn more.
As below, the difference between "= =" and equals is found from So,
So, the content of the two objects that is usually compared to equals, "= =" Compares whether the two are the same thing. In a custom class, if you want to use object comparisons, you can override equals to control the judgment logic as needed.