How do Linq Expressions determine equality?

后端 未结 3 905
长情又很酷
长情又很酷 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:12

    Your test compares expressions. Expressions themselves offer only reference equality; your test will probably show "false". To cheek for semantic equality you would need to do a lot of work, for example - are:

    x => 123
    

    And

    y => 123
    

    Equivalent? As a crude test you can compare ToString(), but this will be exceptionally brittle.

提交回复
热议问题