C# LINQ — “Average”
LINQ (Language-Integrated Query) is a set of integrated C# capabilities which allow queries against data.
Today’s Objective: Learn and use LINQ’s Average method to get the average of a collection of numerical values.
The LINQ Average Method:
The Average method is a very simple and quick way to find the average value from a collection of numerical values, such as a simple list of numbers, or the average level of items from a list of items.
An Example:
Without using LINQ, to get the average of a set of numbers, you would likely do something like this:
But using LINQ, you can simply do:
So simple!
You can also get the average from a custom collection which is not numerical, but contains numerical variables, such as: