Aggregate Data Sources - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Aggregate Data Sources

Demo Code


using System;/*w  w  w . ja va2 s. com*/
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MainClass
    {
        static void Main(string[] args)
        {
            int[] ds1 = { 11, 23, 37, 49, 143 };

            Console.WriteLine("Standard aggregation methods");
            Console.WriteLine("Average: {0}", ds1.Average());
            Console.WriteLine("Count: {0}", ds1.Count());
            Console.WriteLine("Max: {0}", ds1.Max());
            Console.WriteLine("Min: {0}", ds1.Min());
            Console.WriteLine("Sum: {0}", ds1.Sum());

            Console.WriteLine("\nCustom aggregation");
            Console.WriteLine(ds1.Aggregate((total, elem) => total += elem));

            string[] ds2 = { "Oracle", "pear", "PLSQL" };

            Console.WriteLine("\nString concatenation aggregation");
            Console.WriteLine(ds2.Aggregate((len, elem) => len += elem));

        }
    }

Result


Related Tutorials