1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . Linq ;
4
5
using System . Runtime . CompilerServices ;
5
6
6
7
@@ -12,24 +13,16 @@ public class DistinctRootEntityResultTransformer : IResultTransformer, ITupleSub
12
13
private static readonly INHibernateLogger log = NHibernateLogger . For ( typeof ( DistinctRootEntityResultTransformer ) ) ;
13
14
internal static readonly DistinctRootEntityResultTransformer Instance = new DistinctRootEntityResultTransformer ( ) ;
14
15
15
- internal sealed class Identity
16
+ sealed class IdentityComparer < T > : IEqualityComparer < T >
16
17
{
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 )
18
+ public bool Equals ( T x , T y )
25
19
{
26
- Identity that = ( Identity ) other ;
27
- return ReferenceEquals ( entity , that . entity ) ;
20
+ return ReferenceEquals ( x , y ) ;
28
21
}
29
22
30
- public override int GetHashCode ( )
23
+ public int GetHashCode ( T obj )
31
24
{
32
- return RuntimeHelpers . GetHashCode ( entity ) ;
25
+ return RuntimeHelpers . GetHashCode ( obj ) ;
33
26
}
34
27
}
35
28
@@ -40,13 +33,16 @@ public object TransformTuple(object[] tuple, string[] aliases)
40
33
41
34
public IList TransformList ( IList list )
42
35
{
43
- IList result = ( IList ) Activator . CreateInstance ( list . GetType ( ) ) ;
44
- var distinct = new HashSet < Identity > ( ) ;
36
+ if ( list . Count < 2 )
37
+ return list ;
38
+
39
+ IList result = ( IList ) Activator . CreateInstance ( list . GetType ( ) ) ;
40
+ var distinct = new HashSet < object > ( new IdentityComparer < object > ( ) ) ;
45
41
46
42
for ( int i = 0 ; i < list . Count ; i ++ )
47
43
{
48
44
object entity = list [ i ] ;
49
- if ( distinct . Add ( new Identity ( entity ) ) )
45
+ if ( distinct . Add ( entity ) )
50
46
{
51
47
result . Add ( entity ) ;
52
48
}
@@ -59,6 +55,15 @@ public IList TransformList(IList list)
59
55
return result ;
60
56
}
61
57
58
+ internal static List < T > TransformList < T > ( IEnumerable < T > list )
59
+ {
60
+ var result = list . Distinct ( new IdentityComparer < T > ( ) ) . ToList ( ) ;
61
+ if ( log . IsDebugEnabled ( ) )
62
+ {
63
+ log . Debug ( "transformed: {0} rows to: {1} distinct results" , list . Count ( ) , result . Count ) ;
64
+ }
65
+ return result ;
66
+ }
62
67
63
68
public bool [ ] IncludeInTransform ( String [ ] aliases , int tupleLength )
64
69
{
0 commit comments