Average in LINQ Query in Entity Framework Core

Average method is used to calculate the average of any numeric column in database.

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var average = (from product in databaseContext.Products
                               select product).Average(product => product.Price);
                Console.WriteLine("Average : " + average);
            }
        }
    }
}
Average: 16.625

You can use combination of Average method and Where as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var average = (from product in databaseContext.Products
                               where product.Status == true
                               select product).Average(product => product.Price);
                Console.WriteLine("Average : " + average);
            }
        }
    }
}
Average: 15.25

You can use combination of Average method and Where and Select as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var average = (from product in databaseContext.Products
                               where product.Status == true
                               select product.Price).Average();
                Console.WriteLine("Average : " + average);
            }
        }
    }
}
Average: 15.25