«

»

Aug 21

Print this Post

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:

And result of above code is

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:

And result of above code is

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:

And result of above code is

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:

And result of above code is

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:

And result of above code is

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:

And result of above code is

Permanent link to this article: http://www.zappmania.in/2016/08/21/linq-aggregate-methods-c-sharp-example.htm

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">