views:

43

answers:

1

Possible Duplicate:
Most efficient way to test equality of lambda expressions

I was wondering how LINQ expression instances compare for equality. It seems that the expression classes do not implement IEquatable or IComparable or anything alike.

Let me give you some background information: I've implemented a caching mechanism that stores compiled labmda expressions. Currently I'm using the output of Expression.ToString() as the cache key but I would like to find a better and more efficient way of distinguishing expressions.

A: 

Your ToString method is probably the best and easiest way, like any example i've seen that doesn't use ToString, ToString walks through the nodes of the expression and turns them into strings. If you don't want to store the whole string, you can store it's hash. You have to consider carefully however if the number of times you are going to use each compiled expression is worth the overhead of the cache and the ToString used to find the cache value.

marr75