Calculating Server Side Aggregates

10 posts, 0 answers
  1. B
    B avatar
    25 posts
    Member since:
    Aug 2010

    Posted 09 Jan 2018 Link to this post

    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.

     

     

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 10 Jan 2018 Link to this post

    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.
  3. Erik
    Erik avatar
    42 posts
    Member since:
    Aug 2012

    Posted 13 Feb 2018 in reply to Konstantin Dikov Link to this post

    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.

  4. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 15 Feb 2018 Link to this post

    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.
  5. Erik
    Erik avatar
    42 posts
    Member since:
    Aug 2012

    Posted 15 Feb 2018 in reply to Konstantin Dikov Link to this post

    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?

  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 19 Feb 2018 Link to this post

    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.
  7. Erik
    Erik avatar
    42 posts
    Member since:
    Aug 2012

    Posted 21 Feb 2018 in reply to Konstantin Dikov Link to this post

    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.

  8. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 26 Feb 2018 Link to this post

    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.
  9. Erik
    Erik avatar
    42 posts
    Member since:
    Aug 2012

    Posted 26 Feb 2018 in reply to Konstantin Dikov Link to this post

    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.

  10. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 28 Feb 2018 Link to this post

    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.
Back to Top