@@ -12,24 +12,16 @@ public class DistinctRootEntityResultTransformer : IResultTransformer, ITupleSub
12
12
private static readonly INHibernateLogger log = NHibernateLogger . For ( typeof ( DistinctRootEntityResultTransformer ) ) ;
13
13
private static readonly object Hasher = new object ( ) ;
14
14
15
- internal sealed class Identity
15
+ internal sealed class IdentityComparer : IEqualityComparer < object >
16
16
{
17
- internal readonly object entity ;
18
-
19
- internal Identity ( object entity )
20
- {
21
- this . entity = entity ;
22
- }
23
-
24
- public override bool Equals ( object other )
17
+ public new bool Equals ( object x , object y )
25
18
{
26
- Identity that = ( Identity ) other ;
27
- return ReferenceEquals ( entity , that . entity ) ;
19
+ return ReferenceEquals ( x , y ) ;
28
20
}
29
21
30
- public override int GetHashCode ( )
22
+ public int GetHashCode ( object obj )
31
23
{
32
- return RuntimeHelpers . GetHashCode ( entity ) ;
24
+ return RuntimeHelpers . GetHashCode ( obj ) ;
33
25
}
34
26
}
35
27
@@ -40,13 +32,16 @@ public object TransformTuple(object[] tuple, string[] aliases)
40
32
41
33
public IList TransformList ( IList list )
42
34
{
43
- IList result = ( IList ) Activator . CreateInstance ( list . GetType ( ) ) ;
44
- var distinct = new HashSet < Identity > ( ) ;
35
+ if ( list . Count < 2 )
36
+ return list ;
37
+
38
+ IList result = ( IList ) Activator . CreateInstance ( list . GetType ( ) ) ;
39
+ var distinct = new HashSet < object > ( new IdentityComparer ( ) ) ;
45
40
46
41
for ( int i = 0 ; i < list . Count ; i ++ )
47
42
{
48
43
object entity = list [ i ] ;
49
- if ( distinct . Add ( new Identity ( entity ) ) )
44
+ if ( distinct . Add ( entity ) )
50
45
{
51
46
result . Add ( entity ) ;
52
47
}
@@ -77,13 +72,10 @@ public bool IsTransformedValueATupleElement(String[] aliases, int tupleLength)
77
72
78
73
public override bool Equals ( object obj )
79
74
{
80
- if ( obj == null || obj . GetHashCode ( ) != Hasher . GetHashCode ( ) )
81
- {
82
- return false ;
83
- }
84
- // NH-3957: do not rely on hashcode alone.
85
- // Must be the exact same type
86
- return obj . GetType ( ) == typeof ( DistinctRootEntityResultTransformer ) ;
75
+ if ( ReferenceEquals ( obj , this ) )
76
+ return true ;
77
+
78
+ return obj is DistinctRootEntityResultTransformer ;
87
79
}
88
80
89
81
public override int GetHashCode ( )
0 commit comments