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

calculate median

2 Answers 149 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
cvuas
Top achievements
Rank 1
cvuas asked on 24 Feb 2014, 01:41 PM
I am a pure beginner in csharp.

In my report, I have to group the results of an sql query data and calculate the average min., max. and median (I have three grouping categories: category I, II and III). For the average, min and max I can use the AVE-, MIN- and MAX-functions.
To calculate the median, I found the following c#-script:

public static double Median(this IEnumerable<double> list)
        {

            List<double> orderedList = list
                .OrderBy(numbers => numbers)
                .ToList();

            int listSize = orderedList.Count;
            double result;

            if (listSize % 2 == 0) // even
            {
                int midIndex = listSize / 2;
                result = ((orderedList.ElementAt(midIndex - 1) +
                           orderedList.ElementAt(midIndex)) / 2);
            }
            else // odd
            {
                double element = (double)listSize / 2;
                element = Math.Round(element, MidpointRounding.AwayFromZero);

                result = orderedList.ElementAt((int)(element - 1));
            }

            return result;
        }  


As I am an absolute c#-beginner, I don't know how to pass the sql-result of each group to the median-function... :-( I would be very thankful, if someone could provide me any hint.


2 Answers, 1 is accepted

Sort by
0
Hinata
Top achievements
Rank 1
answered on 27 Feb 2014, 09:11 AM

Hello cvuas,

I believe there is no built-in aggregate function in the reporting that will give you the median. In such cases you can implement your own function, but I am afraid it will require some C# knowledge. For more information on how this is done follow these help articles:
http://www.telerik.com/help/reporting/expressions-user-functions.html
http://www.telerik.com/help/reporting/expressions-user-aggregate-functions.html

0
IvanY
Telerik team
answered on 27 Feb 2014, 09:50 AM
Hello Hubert,

In your case you will have to implement a user aggregate function. To do so please check this help article. Once you have implemented your user function you will be able to use it like the rest of the predefined functions in an expression - for more information please check the Expressions help article and the related subarticles.

However please note that since you are a beginner in C# most probably it will be best to catch up a little bit prior to implementing the function. Depending on your current knowledge and previous experience with programming you might want to check the C# Programming Guide, the C# Reference and the C# Tutorials.

Regards,
IvanY
Telerik

New HTML5/JS REPORT VIEWER with MOBILE AND TOUCH SUPPORT available in Telerik Reporting Q3 2013! Get the new Reporting version from your account or download a trial.

Tags
General Discussions
Asked by
cvuas
Top achievements
Rank 1
Answers by
Hinata
Top achievements
Rank 1
IvanY
Telerik team
Share this question
or