Creating your own LINQ extension methods

In Chapter 6, Implementing Interfaces and Inheriting Classes, you learned how to create your own extension methods. To create LINQ extension methods, all you must do is extend the IEnumerable<T> type.

Good PracticePut your own extension methods in a separate class library so that they can be easily deployed as their own assembly or NuGet package.

In either Visual Studio 2017 or Visual Studio Code, open the LinqWithEFCore project or folder, and add a new class file named MyLINQExtensions.cs.

We will look at the Average extension method as an example. Any school child will tell you that average can mean one of three things:

  • Mean: Sum the numbers and divide by the count
  • Mode: The most common number

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.