可能重複:
Most efficient way to test equality of lambda expressions
How to check if two Expression<Func<T, bool>> are the same如何測試表達式平等
如何測試兩個表達式是這樣的樣品一樣
現在怎麼樣以確保expression1等於expres sion2,因爲他們有相同的標準?
可能重複:
Most efficient way to test equality of lambda expressions
How to check if two Expression<Func<T, bool>> are the same如何測試表達式平等
如何測試兩個表達式是這樣的樣品一樣
現在怎麼樣以確保expression1等於expres sion2,因爲他們有相同的標準?
以下是ExpressionEqualityComparer
的代碼,可以顯示如何操作。
https://source.db4o.com/db4o/trunk/db4o.net/Db4objects.Db4o.Linq/Db4objects.Db4o.Linq/Expressions/
如果你想檢查表達式是平等的,不只是他們始終評估以同樣的方式,你可以這樣做:
exp1.ToString() == exp2.ToString()
注意,即使微不足道的變化將導致此返回false,像使它j => j.Contains(firstname)
或使用exp2
從這個類:
public class Test
{
static string firstname;
public static Expression<Func<string, bool>> exp2 = s => s.Contains(firstname);
}
(甚至日ough的lambda表達式看起來相同的代碼中,ToString
秀,一個是使用Test.firstname
,另一種是使用編譯器生成的類的firstname
)
不過,這可能會因您的表現來自有用。