Custom Aggregate Function: ArgumentMax()
Review the documentation on Custom Aggregate Functions for full details on developing and implementing this function.
| Description | Returns the corresponding correspondingValue associated with the maximum value of maximizedValue. |
|---|---|
| Arguments | |
| Namespaces |
|
| Example | To return the name of the most played artist: ArgumentMax({Spins.Plays}, {Artists.Name})Jamiroquai |
Program Code
public class MaxMapAggregator: ICustomAggregator
{
IComparable max;
object maxKey;
public void AddValue(SessionInfo sessionInfo, object value, params object[] args)
{
// Treat nulls as zero
if (value == null)
value = 0;
else if (!(value is IComparable))
{
throw new WrAggregationException(@"Tried to take the maximum of a set
containing a non-comparable value");
}
if (this.max == null || this.max.CompareTo(value) == -1)
{
this.max = (IComparable)value;
// The "key" to associate with the maximum value is passed as the second
// argument to this aggregate function, which shows up here as the first item
// in the args array.
this.maxKey = args[0];
}
}
public object Result(SessionInfo sessionInfo)
{
return this.maxKey;
}
}