如何检查两个前pression<&Func键LT; T,BOOL>>是相同的 [英] How to check if two Expression<Func<T, bool>> are the same

查看:181
本文介绍了如何检查两个前pression<&Func键LT; T,BOOL>>是相同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否在两位前pressions都是一样的找出来呢?

Is it possible to find out if two expressions are the same?

就像给下面四个前pressions:

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;

然后,至少的 <我们/ em>的可以看到:

Then, at least we can see that:


  • A == b

  • A!= C

  • A!= D

  • a == b
  • a != c
  • a != d

不过,我可以做任何事情,在我的code发现了这一点?

But can I do anything to find this out in my code?

在MSDN库,它说,

等于:确定指定的对象是否等于当前对象。 (从继承的对象

Equals: Determines whether the specified Object is equal to the current Object. (Inherited from Object.)

我的猜测意味着至少防爆pression类没有overrided equals方法成为Equatable?所以,你会怎么做呢?还是我要求太多了吗? :P

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

推荐答案

您可以看看类型<一个href=\"https://source.db4o.com/db4o/trunk/db4o.net/Db4objects.Db4o.Linq/Db4objects.Db4o.Linq/Ex$p$pssions/Ex$p$pssionEqualityComparer.cs\">Ex$p$pssionEqualityComparer即在<二手href=\"http://evain.net/blog/articles/2008/02/06/an-elegant-linq-to-db4o-provider-and-a-few-linq-tricks\">Linq到db4o 。它实现接口的IEqualityComparer&下;:T&gt;中,以便这对一般集合可用的,以及用于独立使用

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.

它采用的类型<一个href=\"https://source.db4o.com/db4o/trunk/db4o.net/Db4objects.Db4o.Linq/Db4objects.Db4o.Linq/Ex$p$pssions/Ex$p$pssionComparison.cs\">Ex$p$pssionComparison比较平等两位前pressions和<一个href=\"https://source.db4o.com/db4o/trunk/db4o.net/Db4objects.Db4o.Linq/Db4objects.Db4o.Linq/Ex$p$pssions/Hash$c$cCalculation.cs\">Hash$c$cCalculation,计算散列code从防爆pression。

It uses the type ExpressionComparison to compare two Expressions for equality, and HashCodeCalculation, to compute a hashcode from an Expression.

这一切都包含在您参观前pression树,所以可以pretty昂贵的,如果你反复地做,但是它也可以是很方便。

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.

在code是GPL或 dOCL

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.

这篇关于如何检查两个前pression&LT;&Func键LT; T,BOOL&GT;&GT;是相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆