简体   繁体   中英

How to calculate Moving Average calculations in Dataset Datatable by C#?

Is there any way to do moving average calculations in Dataset Datatable. Is this the best option for calculating moving average calculations by using C sharp? what i am thinking is Datatable looks like excel, because of i am very familier with Excel and vba macros i think this is the best options. I am just beginer in C sharp i don't know other options, so anyone have any information about it, it will very helpful for me. And thanks in advance for your time.

Something as simple as this could do the trick.

static class Extensions
{
     public static IEnumerable<double> MovingAverage(this double[] numbers, int runs)
     {
         for (var i = 0; i < numbers.Length - runs + 1; i++)
             yield return Enumerable.Range(i, runs).Average(idx => numbers[idx]);
     }
}

Use case:

 foreach (var number in new[] {1d, 2d, 7d, 4d, 5}.MovingAverage(2))
     Console.WriteLine(number);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM