Numeric Aggregates: Average : Average « LINQ « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            Random generator = new Random(0);
            int[] numbers = new int[1000];
            for (int i = 0; i < 1000; i++)
            {
                numbers[i] = generator.Next();
            }
            var queryResults = from n in numbers where n > 100 select n;

            Console.WriteLine("Average of Numbers > 100");
            Console.WriteLine(queryResults.Average());

        }
    }








22.19.Average
22.19.1.Using Average to get the average of all values of an integer array.
22.19.2.Average with int array
22.19.3.Using Average to get the average length of the words in the string array.
22.19.4.Grouped Average
22.19.5.Using Average method to calculate the average value from a query
22.19.6.Numeric Aggregates: Average