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.
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.