How do Linq Expressions determine equality?

后端 未结 3 902
长情又很酷
长情又很酷 2021-01-11 16:25

I am considering using a Linq Expression as a key in a dictionary. However, I am concerned that I will get strange results, because I don\'t know how Equality is determined

3条回答
  •  独厮守ぢ
    2021-01-11 17:07

    Comparing any two objects that aren't value types (including an Expression) with == compares object references, so they won't be equal. As a commenter noted, though, a dictionary would be using Equals and GetHashCode to determine equality, which would still by default end up determining that they were not equal.

    You could probably create a class that inherits System.Linq.Expression and override GetHashCode and Equals to use the result somehow, though, and use that as the key for your dictionary.

提交回复
热议问题