Click here to Skip to main content
15,887,214 members
Articles / LINQ

LINQ – Which Are the Aggregation Operators?

Rate me:
Please Sign up or sign in to vote.
4.60/5 (3 votes)
30 Mar 2014CPOL1 min read 4.3K   5  
Which are the Aggregation operators in LINQ

LINQ

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
  • Count/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.

C#
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 runningProcesses.

Similarly, we can find the minimum number of threads, maximum number of threads and the average number of threads on a process.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
-- There are no messages in this forum --