-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountDistinctCustomAggregate.cs
51 lines (44 loc) · 1.96 KB
/
CountDistinctCustomAggregate.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
using DevExpress.Data.Filtering;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
public class CountDistinctCustomAggregate : ICustomAggregateFormattable, ICustomAggregateBrowsable {
static readonly CountDistinctCustomAggregate instance = new CountDistinctCustomAggregate();
public static void Register() {
CriteriaOperator.RegisterCustomAggregate(instance);
}
public static void Unregister() {
CriteriaOperator.UnregisterCustomAggregate(instance);
}
public string Name { get { return nameof(CountDistinct); } }
public int MinOperandCount => -1;
public int MaxOperandCount => -1;
public string Description => "This is a custom aggregate function";
Type ICustomAggregate.ResultType(params Type[] operands) {
return typeof(int);
}
object ICustomAggregate.CreateEvaluationContext() {
return new HashSet<object>();
}
bool ICustomAggregate.Process(object context, object[] operands) {
var ctx = (HashSet<object>)context;
ctx.Add(operands[0]);
return false;
}
object ICustomAggregate.GetResult(object context) {
var ctx = (HashSet<object>)context;
return ctx.Count;
}
string ICustomAggregateFormattable.Format(Type providerType, params string[] operands) {
return string.Format("COUNT(distinct {0})", operands[0]);
}
public static object CountDistinct<T>(IEnumerable<T> collection, Expression<Func<T, object>> arg) {
throw new InvalidOperationException("This method should not be called explicitly.");
}
public bool IsValidOperandCount(int count) {
return true;
}
public bool IsValidOperandType(int operandIndex, int operandCount, Type type) {
return true;
}
}