Skip to content

Commit 81bf5b5

Browse files
committed
Expression Equality Comparer docs
1 parent dcc5024 commit 81bf5b5

File tree

5 files changed

+157
-8
lines changed

5 files changed

+157
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The Expression Tree Toolkit is a collection of helper functions, extension metho
2121

2222
# See It In Action
2323

24-
ExpressionEqualityComparer example on [dotnetfiddle](https://dotnetfiddle.net/ijvLUm)
24+
ExpressionEqualityComparer example on [dotnetfiddle](https://dotnetfiddle.net/O2gVw7)
2525

2626
# Contribution Guidelines
2727
Please use [GitHub Issues](https://github.com/alecsg77/ExpressionTreeToolkit/issues) for bug reports and feature requests.
Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,98 @@
11
# Expression Equality Comparer
22

3-
## Using
3+
The default equality comparison logic between two <xref:System.Linq.Expressions.Expression>s verifies only if they refer to the same instance.
44

5-
## Extends
5+
```csharp
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq.Expressions;
9+
10+
public class Program
11+
{
12+
static HashSet<Expression> expressions;
13+
public static void Main()
14+
{
15+
IEqualityComparer<Expression> comparer = EqualityComparer<Expression>.Default;
16+
expressions = new HashSet<Expression>(comparer);
17+
18+
Console.WriteLine("Expression equality:");
19+
AddExpression((x, y) => x + y);
20+
AddExpression((x, y) => x * y);
21+
AddExpression((x, y) => x + y);
22+
AddExpression((a, b) => a * b);
23+
Console.WriteLine();
24+
}
25+
26+
static void AddExpression(Expression<Func<int,int,int>> exp)
27+
{
28+
if (expressions.Add(exp))
29+
{
30+
Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}",
31+
exp.ToString(), expressions.Count.ToString(), exp.GetHashCode());
32+
}
33+
else
34+
{
35+
Console.WriteLine("An expression equal to {0} is already in the collection.", exp.ToString());
36+
}
37+
}
38+
}
39+
/* This example produces the following output:
40+
*
41+
Expression equality:
42+
Added (x, y) => (x + y), Count = 1, HashCode = 9293733
43+
Added (x, y) => (x * y), Count = 2, HashCode = 60803526
44+
Added (x, y) => (x + y), Count = 3, HashCode = 4729318
45+
Added (a, b) => (a * b), Count = 4, HashCode = 4497
46+
*
47+
*/
48+
```
49+
50+
The <xref:ExpressionTreeToolkit.ExpressionEqualityComparer.Default> implementation of the <xref:ExpressionTreeToolkit.ExpressionEqualityComparer> instead compares each node in the expression tree to determine if two <xref:System.Linq.Expressions.Expression>s are equal.
51+
52+
```csharp
53+
using System;
54+
using System.Collections.Generic;
55+
using System.Linq.Expressions;
56+
57+
public class Program
58+
{
59+
static HashSet<Expression> expressions;
60+
public static void Main()
61+
{
62+
IEqualityComparer<Expression> comparer = ExpressionTreeToolkit.ExpressionEqualityComparer.Default;
63+
expressions = new HashSet<Expression>(comparer);
64+
65+
Console.WriteLine("Expression equality:");
66+
AddExpression((x, y) => x + y);
67+
AddExpression((x, y) => x * y);
68+
AddExpression((x, y) => x + y);
69+
AddExpression((a, b) => a * b);
70+
Console.WriteLine();
71+
}
72+
73+
static void AddExpression(Expression<Func<int,int,int>> exp)
74+
{
75+
if (expressions.Add(exp))
76+
{
77+
Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}",
78+
exp.ToString(), expressions.Count.ToString(), exp.GetHashCode());
79+
}
80+
else
81+
{
82+
Console.WriteLine("An expression equal to {0} is already in the collection.", exp.ToString());
83+
}
84+
}
85+
}
86+
/* This example produces the following output:
87+
*
88+
Expression equality:
89+
Added (x, y) => (x + y), Count = 1, HashCode = 10529038
90+
Added (x, y) => (x * y), Count = 2, HashCode = 35464548
91+
An expression equal to (x, y) => (x + y) is already in the collection.
92+
An expression equal to (a, b) => (a * b) is already in the collection.
93+
*
94+
*/
95+
```
96+
97+
> [!NOTE]
98+
> The comparison logic is different for each <xref:System.Linq.Expressions.ExpressionType>. Please refers to the source code.

docfx/articles/ExpressionIterator.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
# Expression Iterator
2-
3-
## Using
4-
5-
## Extends

docfx/articles/intro.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Introduction to Expression Tree Toolkit
22

3-
## Why
3+
## Why Expression Tree Toolkit?
4+
5+
- Provide a centralized shared point of the most common functionalities around the expression trees, to save time and focus on more business-relevant functionality
6+
- Simplifies the use of expression trees to facilitate their adoption in many contexts other than query and dynamic code generation.
47

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
uid: ExpressionTreeToolkit.ExpressionEqualityComparer
3+
example:
4+
- *content
5+
---
6+
7+
The following example a dictionary collection of objects of type <xref:System.Linq.Expressions.Expression> with the <xref:ExpressionTreeToolkit.ExpressionEqualityComparer.Default> expression equality comparer.
8+
9+
```csharp
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq.Expressions;
13+
using ExpressionTreeToolkit;
14+
15+
public class Program
16+
{
17+
static Dictionary<Expression, String> expressions;
18+
public static void Main()
19+
{
20+
expressions = new Dictionary<Expression, string>(ExpressionEqualityComparer.Default);
21+
22+
Console.WriteLine("Expression equality:");
23+
Expression<Func<int, int, int>> sumXYExpression = (x, y) => x + y;
24+
Expression<Func<int, int, int>> prodXYExpression = (x, y) => x * y;
25+
Expression<Func<int, int, int>> sumABExpression = (a, b) => a + b;
26+
Expression<Func<int, int, int>> diffABExpression = (a, b) => a - b;
27+
AddExpression(sumXYExpression, "x + y");
28+
AddExpression(prodXYExpression, "x * y");
29+
AddExpression(sumABExpression, "a + b");
30+
AddExpression(diffABExpression, "a - b");
31+
Console.WriteLine();
32+
}
33+
34+
static void AddExpression(Expression exp, string name)
35+
{
36+
try
37+
{
38+
expressions.Add(exp, name);
39+
Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}",
40+
name, expressions.Count.ToString(), exp.GetHashCode());
41+
}
42+
catch (ArgumentException)
43+
{
44+
Console.WriteLine("An expression equal to {0} is already in the collection.", name);
45+
}
46+
}
47+
}
48+
/* This example produces the following output:
49+
*
50+
Expression equality:
51+
Added x + y, Count = 1, HashCode = 9265398
52+
Added x * y, Count = 2, HashCode = 261086
53+
An expression equal to a + b is already in the collection.
54+
Added a - b, Count = 3, HashCode = 7571494
55+
*
56+
*/
57+
```

0 commit comments

Comments
 (0)