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

Calculating Server Side Aggregates

9 Answers 485 Views
Grid
This is a migrated thread and some comments may be shown as answers.
B
Top achievements
Rank 1
B asked on 09 Jan 2018, 10:08 PM

I'm absolutely stuck trying to create a server side aggregate.  I can't find any examples or documentation that describes how to do this.

I've seen posts that recommend using AggregateFunction but I think that may be for Telerik grids or changed.  I've tried using an AggregateResult but that won't let me set the member.  

Any help or working examples would be very much appreciated.

 

 

9 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 10 Jan 2018, 07:53 AM
Hello,

Could you please provide more information on the exact requirement that you have? Are you trying to use custom binding and populate the DataSourceResult with custom aggregates or you have any other requirement?

Looking forward to your reply.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Erik
Top achievements
Rank 1
answered on 13 Feb 2018, 07:58 PM

Since the OP seems to have abandoned this question, I would like some help on server side aggregates.

From this posting (https://www.telerik.com/forums/how-do-i-perform-server-side-grouping-and-aggregating#eYcjSUEOi0mzcX-XdQj0cg) it seems that server side aggregates is not supported out of box and requires an extensive custom solution. Is that really true?

I need server operation enabled on my grid due to custom sorting.  I would like to display a count in each header when grouping by specific fields. I have in specific columns.Bound lines:

columns.Bound(c => c.myField).ClientGroupHeaderTemplate("My Group Name: #: value # - #= count #")

 

as well as defined aggregates in the .DataSource

.Aggregates(aggregates =>
             {
                aggregates.Add(p => p.myField).Count();
             })

 

With ServerOperation(false), the aggregate count display just fine for the group header.  But when I re-enable ServerOperation, I then get count undefined error.  I have not been able to find a clear-cut example for server side aggregates.

0
Konstantin Dikov
Telerik team
answered on 15 Feb 2018, 07:34 AM
Hello Erik,

Since it is a custom binding you will have to write the logic for calculating the aggregates and return the format that the Grid expects, but as mentioned in the forum thread that you are referring to, this is a complex operation that should be handled by the developer, following the suggestions from my colleague. 

Notwithstanding, if you need the custom binding for the custom sorting only, you could try to use ToDataSourceResult directly in addition with IComparable for the properties in your model, where the custom sorting logic is needed.

Hope this helps.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Erik
Top achievements
Rank 1
answered on 15 Feb 2018, 01:12 PM

Konstantin,

Would it be possible for you to give brief example or starter code that could be used for calculating a simple aggregate such as counts?  I am not even sure where to start on that.

 

Can you explain more about using ToDataSourceResult directly in addition with IComparable for the properties in my model.  Is there an example you could point me to on that?

0
Konstantin Dikov
Telerik team
answered on 19 Feb 2018, 11:43 AM
Hi Erik,

I am afraid that we do not have example for custom binding with aggregates and creating such example will require to rewrite the code within the ToDataSourceResult method. It might be easier to create the JSON result in a format that the Grid expects without the DataSourceResult object.

As for the ToDataSourceResult functionality (without the custom binding), you can define the custom IComparable for the property that you need to customize as shown below:
Hope this helps.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Erik
Top achievements
Rank 1
answered on 21 Feb 2018, 10:30 PM

Konstantin,

From what I've gathered, server-side aggregates (even a simple count) is exceedingly difficult and Telerik does not provide examples or support for doing so. OK fine, I won't ask any more on that.

 

I am not fully following your ToDataSourceResult solution.  In my case I've already implemented something very similar to the  CustomServerBinding example as shown in the CustomServerBindingController.cs where I have ApplySorting, ApplyGrouping, ApplyPaging that are called with ServerOperation(true).  In my AddSortExpression method, I call my own custom IComparer class to handle the custom sorting logic.  This all works great EXCEPT I get an error when trying to display aggregate counts for groups.

0
Konstantin Dikov
Telerik team
answered on 26 Feb 2018, 09:25 AM
Hello Erik,

My suggestion for using the ToDataSourceResult will require to pass IEnumerable to the method and define custom IComparable to the properties over which you need the custom sort criteria. When the ToDataSourceResult executes the query it should use the custom IComparable and still calculate the aggregates correctly. Nevertheless, this will require to pass all records to the method.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Erik
Top achievements
Rank 1
answered on 26 Feb 2018, 01:35 PM

Can you please provide example passing IEnumerable and defining custom IComparable to the properties over which to custom sort?

 

Like I said before I am currently basing my usage from the CustomServerBinding example where I have a IEnumerable object named data returned from the ApplyGrouping method and I am then executing

 

var result = new DataSourceResult()
            {
            Data = data,
            Total = total
            };
 
return Json(result, JsonRequestBehavior.AllowGet);

 

from the ActionResult method.

0
Konstantin Dikov
Telerik team
answered on 28 Feb 2018, 01:59 PM
Hi Erik,

In the following MSDN article you could find example for custom sorting by implementing IComparable:
This should be implemented for the property in the model for which you need the custom sorting. Afterwords you need to pass the entire data set as IEnumerable to the ToDataSourceResult method, which will use the custom CompareTo for sorting that particular field:
public ActionResult Remote_Binding_Orders_Read([DataSourceRequest]DataSourceRequest request)
{
    return Json(myDataAsIEnumerable.ToDataSourceResult(request));
}

Nevertheless, I should say that the custom CompareTo will not be applied to groups, because the internal logic of the ToDataSourceResult creates different objects for the group items.

Regarding the custom binding, as another solution you could try to manually create the JSON structure that the Grid expects, based on the DataSourceRequest, but this is again something for which we do not have any examples.


Regards,
Konstantin Dikov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
B
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Erik
Top achievements
Rank 1
Share this question
or