In the last blog post on LINQ, we have discussed about different Element Operators. You can read that article here. In this article, we will go over different Aggregation Operators in LINQ.
We know that every good querying language comes with Aggregation Operators and LINQ certainly provides some of them which are as follows:
Average – Calculates the average value in a sequence
Long Count – Counts the elements in a sequence, overload accepts a predicate
Max - Returns the maximum value in a sequence
Min - Returns the minimum value in a sequence
Sum - Calculates the sum of values in a sequence
Let’s look at an example. Look at all software running on a machine using the
Process class and we can produce a summary report about them.
Process runningProcesses = Process.GetProcesses();
var summary = new
ProcessCount = runningProcesses .Count(),
WorkerProcessCount = runningProcesses.Count(
p => p.ProcessName == "w3wp"),
TotalThreads = runningProcesses.Sum(p => p.Threads.Count),
MinThreads = runningProcesses.Min(p => p.Threads.Count),
MaxThreads = runningProcesses.Max(p => p.Threads.Count),
AvgThreads = runningProcesses.Average(p => p.Threads.Count)
So the summary report includes
ProcessCount which will give the count of running processes.
WorkerProcessCount is the count of those processes whose name=”
w3wp”, that’s the worker processes for IIS where ASP.NET applications would run.
Then we will get the
TotalThreads running on all the applications on the machine by summing up the
Threads.Count property on
Similarly, we can find the minimum number of threads, maximum number of threads and the average number of threads on a process.