Linq表达式如何确定平等? [英] How do Linq Expressions determine equality?
问题描述
我正在考虑使用Linq Expression作为字典中的关键字。然而,我担心我会得到奇怪的结果,因为我不知道平等是如何由Linq表达式决定的。
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 by Linq expressions.
派生自表达式的类比较值相等或引用相等吗?或者换句话说,
Does a class derived from Expression compare value equality or reference equality? Or in other words,
Expression<Func<object>> first = () => new object();
Expression<Func<object>> second = ()=>new object();
bool AreTheyEqual = first == second;
推荐答案
您的测试比较表达式 。表达本身只提供参考平等;您的测试可能会显示假。例如,为了面对语义平等,你需要做很多工作,是:
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
和
y => 123
等效?作为一个粗糙的测试,您可以比较ToString(),但这将是非常脆弱的。
Equivalent? As a crude test you can compare ToString(), but this will be exceptionally brittle.
这篇关于Linq表达式如何确定平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!