如何检查两个表达式< Func< T,bool>>是相同的 [英] How to check if two Expression<Func<T, bool>> are the same
问题描述
是否可以找出两个表达式是否相同?
给出以下四个表达式:
表达式& int,bool>>> a = x =>假;
表达式< Func< int,bool>>> b = x =>假;
表达式< Func< int,bool>>> c = x =>真正;
表达式< Func< int,bool>>> d = x => x == 5;
然后,至少我们可以看到:
-
a == b
-
a!= c
-
a!= d
但是我可以在代码中找到这些东西吗?
查看msdn库,在那里它说
等于
:确定指定的Object是否等于当前Object
。 (继承自Object
。)
我猜是指至少Expression类没有超过equals方法成为Equatable?那么你该怎么做呢?还是我在这边问太多了? :p
你可以看一下类型 ExpressionEqualityComparer ://evain.net/blog/articles/2008/02/06/an-elegant-linq-to-db4o-provider-and-a-few-linq-tricksrel =nofollow noreferrer> Linq to db4o 。它实现了IEqualityComparer< T>的接口,因此它可用于通用集合,也可用于独立使用。
它使用类型 ExpressionComparison 进行比较两个表达式相等,而 HashCodeCalculation ,以计算表达式的哈希码。
这一切都涉及访问表达式树,因此如果你反复做,但它也可以很方便。
代码可以在GPL或 dOCL
例如,这是你的测试: / p>
using System;
使用System.Linq.Expressions;
使用Db4objects.Db4o.Linq.Expressions;
class Test {
static void Main()
{
表达式< Func< int,bool>> a = x =>假;
表达式< Func< int,bool>>> b = x =>假;
表达式< Func< int,bool>>> c = x =>真正;
表达式< Func< int,bool>>> d = x => x == 5;
Func< Expression,Expression,bool> eq =
ExpressionEqualityComparer.Instance.Equals;
Console.WriteLine(eq(a,b));
Console.WriteLine(eq(a,c));
Console.WriteLine(eq(a,d));
}
}
它确实打印True,False,False。 / p>
Is it possible to find out if two expressions are the same?
Like given the following four expressions:
Expression<Func<int, bool>> a = x => false;
Expression<Func<int, bool>> b = x => false;
Expression<Func<int, bool>> c = x => true;
Expression<Func<int, bool>> d = x => x == 5;
Then, at least we can see that:
a == b
a != c
a != d
But can I do anything to find this out in my code?
Took a peek in the msdn library, where it says that
Equals
: Determines whether the specified Object is equal to the currentObject
. (Inherited fromObject
.)
which I guess means that at least the Expression class hasn't overrided the equals method to become Equatable? So how would you do this? Or am I asking too much here? :p
You can have a look at the type ExpressionEqualityComparer that is used inside Linq to db4o. It implements the interface IEqualityComparer<T>, so it's usable for generic collections, as well as for a standalone usage.
It uses the type ExpressionComparison to compare two Expressions for equality, and HashCodeCalculation, to compute a hashcode from an Expression.
It all involves visiting the expression tree, so it can be pretty costly if you do it repeatedly, but it can also be quite handy.
The code is available under the GPL or the dOCL
For instance, here's your test:
using System;
using System.Linq.Expressions;
using Db4objects.Db4o.Linq.Expressions;
class Test {
static void Main ()
{
Expression<Func<int, bool>> a = x => false;
Expression<Func<int, bool>> b = x => false;
Expression<Func<int, bool>> c = x => true;
Expression<Func<int, bool>> d = x => x == 5;
Func<Expression, Expression, bool> eq =
ExpressionEqualityComparer.Instance.Equals;
Console.WriteLine (eq (a, b));
Console.WriteLine (eq (a, c));
Console.WriteLine (eq (a, d));
}
}
And it indeed prints True, False, False.
这篇关于如何检查两个表达式< Func< T,bool>>是相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!