Today, we will learn about Linq aggregate functions. Linq provides various aggregate functions like Min(), Max(), Sum(), Count(), Average(), Aggregate().
Here, we will learn about Min() function provided by Linq. As the name suggests, the Min() function gives the minimum value from the list or collection. Linq made it very easy to find out min value from the list as earlier we have to write a bit of coding for finding the min value.
int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int min = numbers.Min();
As we can see we have used the Min() function for it and it will give us the min value from the list we have provided.
Example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqDemoes { class Program { static void Main(string[] args) { int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int min = numbers.Min(); Console.WriteLine("The Minimum Number is {0}", min); Console.ReadLine(); } } }
In this example, we have made an array and stored it in numbers array and find the min value using the Min() function of Linq.
Output:
The Minimum Number is 12
Here, we will learn about Max() function provided by Linq. As the name suggests, the Max() function gives the maximum value from the list or collection. Linq made it very easy to find out max value from the list as earlier we have to write a bit of coding for finding the max value.
int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int max = numbers.Max();
As we can see we have used the Max() function for it and it will give us the max value from the list we have provided.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqDemoes { class Program { static void Main(string[] args) { int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int max = numbers.Max(); Console.WriteLine("The Maximum Number is {0}", max); Console.ReadLine(); } } }
In this example, we have made an array and stored it in numbers array and find the max value using the Max() function of Linq.
Output:
The Maximum Number is 98
Here, we will learn about the Sum() function provided by Linq. As the name suggests, the Sum() function sums up the values from the list or collection. Linq made it very easy to sum the values of the list as earlier we have to write a loop for making the sum.
int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int sum = numbers.Sum();
Example
Here, we will sum all the numbers of the array using the Linq sum function
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqDemoes { class Program { static void Main(string[] args) { int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int sum = numbers.Sum(); Console.WriteLine("Sum is {0}", sum); Console.ReadLine(); } } }
Output:
Sum is 507
Here, we will learn about the Count() function provided by Linq. As the name suggests, the Count() function counts the number of items from the list or array
int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int count = numbers.Count();
Example
Here, we will count all the numbers of the array using the Linq count function
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqDemoes { class Program { static void Main(string[] args) { int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int count = numbers.Count(); Console.WriteLine("Number of items are {0}", count); Console.ReadLine(); } } }
Output:
Number of items are 10
Here, we will learn about the Average() function provided by Linq. As the name suggests, the Average() function finds the average of the number of items in the list or collection
int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; int average = numbers.Average();
Example
Here, we will find the average of all the numbers of the array using the Linq average function
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinqDemoes { class Program { static void Main(string[] args) { int[] numbers = { 50,25,12,63,45,98,63,52,47,52 }; double average = numbers.Average(); Console.WriteLine("Average is {0}", average); Console.ReadLine(); } } }
Output:
Average is 50.7
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular