Sunday, August 21, 2016

LINQ Aggregate Methods C#-Example

LINQ Aggregate Methods C#


LINQ is language integrated query which allow programmers to query various objects without worrying about underlying implementations of these objects.


There are number of LINQ Aggregate methods defined in Enumerable class


  • Enumerable.Sum

  • Enumerable.Max

  • Enumerable.Min

  • Enumerable.Count

  • Enumerable.LongCount

  • Enumerable.Average

  • Enumerable.Aggregate

lets see them one by one.


Enumerable.Sum


Enumerable.Sum is extension method from System.Linq namespace. It returns sum of numeric values in collection.

Lets see this in below code example:



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

namespace Zappmania.Example.LINQ.AggregateMethods


public class Program

public static void Main(string[] args)

//Example - sum of values from list of integer numbers.
var intValueList = new List<int> 2, 3, 5, 8;

int intResult = intValueList.Sum();

Console.Write("The result is " + intResult);

// Example - sum of values from list of decimal numbers.

var decimalValueList = new List<decimal> 8.9m, 12.2m, 18.19m, 40.3m ;
decimal decimalResult = decimalValueList.Sum();
Console.Write("The result is " + decimalResult);


//Example - Sum on empty collection returns 0.

var emptyList = new List<int>(); // empty list
var emptyResult = emptyList.Sum();
Console.Write("The result is " + emptyResult);

//Example - Gets sum of values from list of nullable integers.

var nullableIntValueList = new List<int?> 2, 9, null, 15 ;
int? sum = nullableIntValueList.Sum();
Console.Write("The result is " + sum);

Console.ReadLine();





And result of above code is



The result is 18
The result is 79.59
The result is 0
The result is 26


Enumerable.Max


Enumerable.Max is extension method from System.Linq namespace. It returns maximal value of numeric collection.

Lets see this in below code example:



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

namespace Zappmania.Example.LINQ.AggregateMethods


public class Program

public static void Main(string[] args)

//Example - Max value from list of integer numbers.
var intValueList = new List<int> 2, 3, 5, 8;
int intResult = intValueList.Max();
Console.WriteLine("The result is " + intResult);

// Example - Max value from list of decimal numbers.

var decimalValueList = new List<decimal> 8.9m, 12.2m, 18.19m, 40.3m ;
decimal decimalResult = decimalValueList.Max();
Console.WriteLine("The result is " + decimalResult);


//Example - Max on empty collection returns 0.

//var emptyList = new List<int>(); // empty list
//var emptyResult = emptyList.Max(); //throws exception
//Console.WriteLine("The result is " + emptyResult);

//Example - Gets sum of values from list of nullable integers.

var nullableIntValueList = new List<int?> 2, 9, null, 15 ;
int? max = nullableIntValueList.Max();
Console.WriteLine("The result is " + max);

Console.ReadLine();




And result of above code is




The result is 8
The result is 40.3
The result is 15


Enumerable.Min


Enumerable.Min is extension method from System.Linq namespace. It returns minimal value of numeric collection.


Lets see this in below code example:



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

namespace Zappmania.Example.LINQ.AggregateMethods


public class Program

public static void Main(string[] args)

//Example - Min value from list of integer numbers.
var intValueList = new List<int> 2, 3, 5, 8;
int intResult = intValueList.Min();
Console.WriteLine("The result is " + intResult);

// Example - Min value from list of decimal numbers.

var decimalValueList = new List<decimal> 8.9m, 12.2m, 18.19m, 40.3m ;
decimal decimalResult = decimalValueList.Min();
Console.WriteLine("The result is " + decimalResult);


//Example - Min on empty collection returns 0.

//var emptyList = new List<int>(); // empty list
//var emptyResult = emptyList.Min(); //throws exception
//Console.WriteLine("The result is " + emptyResult);

//Example - Gets sum of values from list of nullable integers.

var nullableIntValueList = new List<int?> 2, 9, null, 15 ;
int? Min = nullableIntValueList.Min();
Console.WriteLine("The result is " + Min);

Console.ReadLine();




And result of above code is




The result is 2
The result is 8.9
The result is 2


Enumerable.Count


Enumerable.Count is extension method from System.Linq namespace. It counts number of items in collection.

Lets see this in below code example:


 

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

namespace Zappmania.Example.LINQ.AggregateMethods


public class Program

public static void Main(string[] args)

//Example - Counts number of items in a collection.
var intValueList = new List 2, 3, 5, 8;
int intResult = intValueList.Count();
Console.WriteLine("The result is " + intResult);

//Example - Counts number of items in a collection.
var decimalValueList = new List 8.9m, 12.2m, 18.19m, 40.3m ;
decimal decimalResult = decimalValueList.Count();
Console.WriteLine("The result is " + decimalResult);

//Example - Counts number of items in a collection.
var strValueList = new List "A", "B", "C" , "D", "E";
var strResult = strValueList.Count();
Console.WriteLine("The result is " + strResult);

//Example - Count on empty collection returns 0.

var emptyList = new List(); // empty list
var emptyResult = emptyList.Count;
Console.WriteLine("The result is " + emptyResult);

//Example - Counts number of items in nullable collection..

var nullableIntValueList = new List 2, 9, null, 15 ;
int? count = nullableIntValueList.Count();
Console.WriteLine("The result is " + count);

Console.ReadLine();





And result of above code is



The result is 4
The result is 4
The result is 5
The result is 0
The result is 4

Enumerable.Average


Enumerable.Average is extension method from System.Linq namespace. It computes average value of numeric collection.

Lets see this in below code example:


 

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

namespace Zappmania.Example.LINQ.AggregateMethods


public class Program

public static void Main(string[] args)

//Example - Computes average of int values items in a collection.
var intValueList = new List 2, 3, 5, 8;
var intResult = intValueList.Average();
Console.WriteLine("The result is " + intResult);

//Example - Computes average of decimal values items in a collection.
var decimalValueList = new List 8.9m, 12.2m, 18.19m, 40.3m ;
decimal decimalResult = decimalValueList.Average();
Console.WriteLine("The result is " + decimalResult);



//Example - Computes average of nullable int values items in a collection.

var nullableIntValueList = new List 2, 9, null, 15 ;
var average = nullableIntValueList.Average();
Console.WriteLine("The result is " + average);

Console.ReadLine();





And result of above code is




The result is 4.5
The result is 19.8975
The result is 8.66666666666667


Enumerable.Aggregate


Enumerable.Aggregate is C# version of fold or reduce function. It is extension method from System.Linq namespace.

Aggregate method applies a function to each item of a collection. For example, let’s have collection 8, 5, 4, 3 and the function Add (operator +) it does (((8+5)+4)+3).

Lets see this in below code example:


 

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

namespace Zappmania.Example.LINQ.AggregateMethods


public class Program

public static void Main(string[] args)

//Example - Sum using Aggregate method.
//This example use Aggregate method overload with only one parameter func.
//Into the func parameter there is passed lambda expression (anonymous method) which adds two numbers.
var intValueList = new List 2, 3, 5, 8;
var intResult = intValueList.Aggregate(func: (result, item) => result + item);
Console.WriteLine("The result is " + intResult);

//Example - Sum using Aggregate method.
//This example use Aggregate method overload with only one parameter func.
//Into the func parameter there is passed lambda expression (anonymous method) which adds two numbers.
var decimalValueList = new List 8.9m, 12.2m, 18.19m, 40.3m ;
var decimalResult = decimalValueList.Aggregate(func: (result, item) => result + item);
Console.WriteLine("The result is " + decimalResult);

//Example - there is passed named method Add instead of lambda expression.

intResult = intValueList.Aggregate(func: Add);
Console.WriteLine("The result is " + intResult);

decimalResult = decimalValueList.Aggregate(func: Add);
Console.WriteLine("The result is " + decimalResult);


Console.ReadLine();

private static int Add(int x, int y) return x + y;
private static decimal Add(decimal x, decimal y) return x + y;





And result of above code is




The result is 18
The result is 79.59
The result is 18
The result is 79.59



LINQ Aggregate Methods C#-Example

No comments:

Post a Comment