forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistinctRootEntityResultTransformer.cs
62 lines (52 loc) · 1.35 KB
/
DistinctRootEntityResultTransformer.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
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using Iesi.Collections.Generic;
namespace NHibernate.Transform
{
[Serializable]
public class DistinctRootEntityResultTransformer : IResultTransformer
{
private static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof(DistinctRootEntityResultTransformer));
internal sealed class Identity
{
internal readonly object entity;
internal Identity(object entity)
{
this.entity = entity;
}
public override bool Equals(object other)
{
Identity that = (Identity) other;
return ReferenceEquals(entity, that.entity);
}
public override int GetHashCode()
{
return RuntimeHelpers.GetHashCode(entity);
}
}
public object TransformTuple(object[] tuple, string[] aliases)
{
return tuple[tuple.Length - 1];
}
public IList TransformList(IList list)
{
IList result = (IList)Activator.CreateInstance(list.GetType());
ISet<Identity> distinct = new HashedSet<Identity>();
for (int i = 0; i < list.Count; i++)
{
object entity = list[i];
if (distinct.Add(new Identity(entity)))
{
result.Add(entity);
}
}
if (log.IsDebugEnabled)
{
log.Debug(string.Format("transformed: {0} rows to: {1} distinct results",
list.Count, result.Count));
}
return result;
}
}
}