Creating LINQ Extension Method
Explore how to create custom LINQ extension methods by extending IEnumerable to calculate mean, mode, and median averages. Understand the nuances of implementing these methods separately from IQueryable and practice chaining them in real-world queries. This lesson helps you customize and extend LINQ functionality for more versatile data manipulation in your C# projects.
To create LINQ extension methods, all we must do is extend the IEnumerable<T> type.
Note: Put our own extension methods in a separate class library so that they can be easily deployed as their own assembly or NuGet package.
Improving the Average extension method
We will improve the Average extension method as an example. A well-educated school child will tell us that average can mean one of three things:
Mean: Sum the numbers and divide by the count.
Mode: The most ...