-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLINQEqualityComparer.cs
52 lines (42 loc) · 1.1 KB
/
LINQEqualityComparer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// LinqPad Environment
void Main()
{
var itemList = new List<Tool>()
{
new Tool(){ ID = 1, Name = "Pump", ProductionDate = DateTime.Today },
new Tool() { ID = 2, Name = "Toy", ProductionDate = DateTime.Today.AddDays(-63) },
new Tool() { ID = 2, Name = "Toy", ProductionDate = DateTime.Today.AddDays(-63) },
null,
new Tool() { ID = 3, Name = "Faucet", ProductionDate = DateTime.Today.AddDays(-3) },
null,
null
};
var comparer = new ToolComparer();
var distList = itemList.Distinct(comparer);
distList.Dump();
}
public class Tool
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime ProductionDate { get; set; }
}
public class ToolComparer : IEqualityComparer<Tool>
{
// Second, once a similar GetHashCode returns, Eqauls() is checked.
public bool Equals(Tool x, Tool y)
{
if(ReferenceEquals(x,y))
return true;
return
x.Name == y.Name &&
x.ProductionDate == y.ProductionDate;
}
// First, itemList element is compared against GetHasCode of ToolComparer
public int GetHashCode(Tool obj)
{
if(obj is null)
return -1;
return obj.ID;
}
}