Aggregate Operators In LINQ
In this article we will discuss about the Aggregate Operators in LINQ.
The Aggregate Operators in Linq allow us to perform simple math operations over the elements in a sequence.
There are mainly six types of Set Operators in Linq.
Aggregate Operators in Linq
Introduction
In this article we will discuss about the Aggregate Operators in LINQ. The Aggregate Operators in Linq allow us to perform simple math operations over the elements in a sequence.
There are mainly six types of Set Operators in Linq.
1. Count - Count the elements in a sequence.
2. LongCount - Count the elements in a very, very long sequence.
3. Sum - Add the elements in a sequence
4. Min - Find the smallest element in a sequence.
5. Max - Find the largest element in a sequence.
6. Average - Find the average value in a sequence.
Now lets discuss all of them one by one.
1. Countpublic void CountFunction()
{
int[] seq1 = { 1, 2, 3, 5, 4, 5, 2, 1 };
var count1 = seq1.Count();
Console.WriteLine("Count of seq1 is : " + count1);
var count2 = "pa55w0rd".Count (c => char.IsDigit (c));
Console.WriteLine("Count with predicate is : " + count2)
}
Output
Count of seq1 is : 8
Count with predicate is : 3
The Count operator return the number of elements in a sequence. The return value is an integer.
2. LongCountpublic void LongCountFunction()
{
int[] seq1 = { 258797878, 25447855, 14478454 };
var longCount = seq1.LongCount();
Console.WriteLine("Count of seq1 is : " + longCount);
}
Output
Count of seq1 is : 3
The LongCount operator provides the same basic functionality as of Count, but allows you to work with an Int64. It Count the elements in a very, very long sequence. The key difference is that the result is a long, rather than a standard integer.
3. Sumpublic void SumFunction()
{
int[] seq1 = { 1, 2, 3 };
var sum = seq1.Sum();
Console.WriteLine("Summation of the elements in array is : " + sum);
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
sum = names.Sum (s => s.Length);
Console.WriteLine("Summation of the elements in string array is : " + sum);
}
Output
Summation of the elements in array is : 6
Summation of the elements in string array is : 19
The Sum method totals the values in a sequence. When used with a collection that contains only numeric values like in a "seq1", the method can be used with no arguments but if you are working with non numeric types like in "names" then the overload function comes into play which accepts a single parameter.
4. Minpublic void MinFunction()
{
int[] seq1 = { 5, 2, 3 };
var minValues = seq1.Min();
Console.WriteLine("Minimum of an array is : ");
}
Output
Minimum of an array is : 2
The Min() Function will find the smallest element in a sequence.
5. Maxpublic void MaxFunction()
{
int[] seq1 = { 3, 2, 5 };
var maxValues = seq1.Max();
Console.WriteLine("Maximum of an array is : ");
}
Output
Maximum of an array is : 5
The Max() Function will find the largest element in a sequence.
6. Averagepublic void IntersectFunction()
{
int[] seq1 = { 1, 2, 3 };
var average = seq1.Average();
Console.WriteLine("Average of seq1 is : " + average);
}
Output
Average of seq1 is : 2
This operator finds the mean of the values in a sequence or we can say that it finds the average value in a sequence.Feedback
If you have any queries regarding Conversion Operators, please do ask me.
Thank for useful article . An aggregate operator's main purpose is to return a single value; aggregate operators include Aggregate, Average, Count, LongCount, Max, Min and Sum.
InvDemo = new List();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace UsingLinqAggregateOperators
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
List
InvDemo.Add(new ProductAmount
{
Category = "Desktop Computer Accessories",
Product = "Keyboard",
Quantity = 200,
Order = 300
});
InvDemo.Add(new ProductAmount
{
Category = " Accessories",
Product = " Speakers",
PtQuantity = 200,
Order = 100
});
InvDemo.Add(new ProductAmount)
{
Category = " Accessories",
Produc = " Speakers",
Quantity = 200,
Order = 100
});
InvDemo.Add(new ProductAmount
{
Name = "Printers",
Product = "Wireless Printer",
Quantity = 200,
Order = 200
});
InvDemo.Add(new ProductAmount)
{
Category = "Printers",
Product = "Wireless Printer",
Quantity = 300,
Order = 500
});
var InventorySProcess = from p in InvDemo
group p by p.Product into g
let maxOrder = g.Max(p => p.Order)
select new {
Category = g.Key,
TotalUnitsInStock = g.Sum(p => p.Quantity),
TotalRequiredOrders = g.Sum(p => p.Order),
Difference = g.Sum(p => p.Quantity) - g.Sum(p => p.Order),
//Paying attention if we need to increase quantity to meet order
Attention = g.Sum(p => p.Quantity) < g.Sum(p => p.Order) ? true : false,
MaximumOrderQuantity = g.Max(p => p.Order),
MinimumOrderQuantity = g.Min(p => p.Order) };
dataGridView1.DataSource = InventorySProcess.ToList();
}
}
public class ProductAmount
{
public ProductAmount()
{
}
public string Category { get; set; }
public string Product { get; set; }
public int Quantity { get; set; }
public int Order { get; set; }
}
}