A custom equality comparer for comparing LINQ expressions.
Comparing expressions using Equals only checks reference equality. Therefore the following test would fail:
Expression<Func<Order, object>> x = order => order.Customer.Address;
Expression<Func<Order, object>> y = order => order.Customer.Address;
Assert.True(x.Equals(y)); // will fail
To solve this problem, this repository introduces an implementation, IEqualityComparer<Expression>ExpressionEqualityComparer, which can be used to compare seperate expression trees to see whether they are essentially the same.
The following example demostrates the use of this class:
Expression<Func<Order, bool>> x = order => order.Customer.Name == "Joe Bloggs";
Expression<Func<Order, bool>> y = order => order.Customer.Name == "Joe Bloggs";
var comparer = new ExpressionEqualityComparer();
Assert.True(comparer.Equals(x, y)); // will pass
The implementaion makes heavy use of the ExpressionVisitor class to determine whether two expression trees are equal. As the nodes in the expression tree are traversed, individual nodes are compared for equality.
This repository has been published to the public nuget.org feed: https://www.nuget.org/packages/yesmarket.Linq.Expressions/