This is a migrated thread and some comments may be shown as answers.

Custom Aggregate on a field which is of List Type

3 Answers 125 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Arpit
Top achievements
Rank 1
Arpit asked on 12 Jun 2012, 11:35 AM
I have these classes Task and ResourceHour
public class Task {
    public Task() {
        this.TotalHoursByResource = new List<ResourceHour>();
    }
 
    public int ID {get;set;}
    public string Name { get; set; }
    public decimal TotalHours { get; set; }
    public List<ResourceHour> TotalHoursByResource { get; set; }
}
 
public class ResourceHour {
    public string Resource { get; set; }
    public string WorkingHourType { get; set; }
    public double Hours { get; set; }
}

A list of Task objects is bound to a RadGridView and I want to create a custom aggregate on Property "TotalHoursByResource" which is a list of ResourceHour objects.

When i try to do it as below, it gives an error
No generic method '' on type '' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

public class HoursAggregate : EnumerableSelectorAggregateFunction {
    protected override string AggregateMethodName {
        get { return "GetAggregateHours"; }
    }
    protected override Type ExtensionMethodsType {
        get { return typeof(HoursAggregateType); }
    }
 
}
 
public static class HoursAggregateType {
    public static string GetAggregateHours<T>(IEnumerable<Task> source, Func<Task, List<ResourceHour>> selector) where T : Task {
        return "Some string from TotalHoursByResource";
    }
}


Any ideas if I can create a custom aggregate function to run on a property which itself is a list??

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 12 Jun 2012, 12:00 PM
Hello,

 You could try to create a general aggregate function like so:

  var aggregate = new AggregateFunction<Player, int>()
{
   AggregationExpression = players =>
    players.Where(player => player.ToString().StartsWith("B")).Select(x => x.Number).Sum(),
   Caption = "Total B"
  
};

In the code snippet, the "Player" is the type of the DataContext of the row, "int" is the type of the returned sum. The function will return the sum of the 'Number' values in all the listed Players. 

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Arpit
Top achievements
Rank 1
answered on 12 Jun 2012, 12:08 PM
Thanks for the quick response Didie.

TotalHoursByResource is a  property of type List<ResourceHour> in my DataContext object Task.

Inside this aggregate function, I want to iterate this list TotalHoursByResource and build a string out of it which can be displayed in group header.

Is it possible that way?
0
Dimitrina
Telerik team
answered on 13 Jun 2012, 04:58 PM
Hello,

The Sum function that I have provided is just to illustrate how you could set the generic aggregate function. It is possible to change the aggregate expression as you wish. You could build it for your concrete property. For example you could check this forum.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Arpit
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Arpit
Top achievements
Rank 1
Share this question
or