Having in LINQ MeThods in Entity Framework Core

Having is used to specify an additional filtering condition on the result of a grouping data from a database using Entity Framework Core.

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var groups = databaseContext.Products.GroupBy(product => product.CategoryId).Select(group => new
                {
                    group.Key,
                    CountProduct = group.Count(),
                    SumQuantities = group.Sum(productOfGroup => productOfGroup.Quantity),
                    MinPrice = group.Min(productOfGroup => productOfGroup.Price),
                    MaxPrice = group.Max(productOfGroup => productOfGroup.Price),
                    AvgPrice = group.Average(productOfGroup => productOfGroup.Price)
                }).Where(group => group.SumQuantities > 41).ToList();
                groups.ForEach(group =>
                {
                    Console.WriteLine("Category Id: " + group.Key);
                    Console.WriteLine("Count Product: " + group.CountProduct);
                    Console.WriteLine("Sum Quantities: " + group.SumQuantities);
                    Console.WriteLine("Min Price: " + group.MinPrice);
                    Console.WriteLine("Max Price: " + group.MaxPrice);
                    Console.WriteLine("Avg Price: " + group.AvgPrice);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Category Id: 2
Count Product: 2
Sum Quantities: 42
Min Price: 4
Max Price: 15
Avg Price: 9.5
---------------------
Category Id: 3
Count Product: 3
Sum Quantities: 63
Min Price: 17
Max Price: 43
Avg Price: 26.333333333333332
---------------------

You can use combination of Having and Skip method as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var groups = databaseContext.Products.GroupBy(product => product.CategoryId).Select(group => new
                {
                    group.Key,
                    CountProduct = group.Count(),
                    SumQuantities = group.Sum(productOfGroup => productOfGroup.Quantity),
                    MinPrice = group.Min(productOfGroup => productOfGroup.Price),
                    MaxPrice = group.Max(productOfGroup => productOfGroup.Price),
                    AvgPrice = group.Average(productOfGroup => productOfGroup.Price)
                }).Where(group => group.SumQuantities > 41).Skip(1).ToList();
                groups.ForEach(group =>
                {
                    Console.WriteLine("Category Id: " + group.Key);
                    Console.WriteLine("Count Product: " + group.CountProduct);
                    Console.WriteLine("Sum Quantities: " + group.SumQuantities);
                    Console.WriteLine("Min Price: " + group.MinPrice);
                    Console.WriteLine("Max Price: " + group.MaxPrice);
                    Console.WriteLine("Avg Price: " + group.AvgPrice);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Category Id: 3
Count Product: 3
Sum Quantities: 63
Min Price: 17
Max Price: 43
Avg Price: 26.333333333333332
---------------------

You can use combination of Having and Skip method and Take method as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var groups = databaseContext.Products.GroupBy(product => product.CategoryId).Select(group => new
                {
                    group.Key,
                    CountProduct = group.Count(),
                    SumQuantities = group.Sum(productOfGroup => productOfGroup.Quantity),
                    MinPrice = group.Min(productOfGroup => productOfGroup.Price),
                    MaxPrice = group.Max(productOfGroup => productOfGroup.Price),
                    AvgPrice = group.Average(productOfGroup => productOfGroup.Price)
                }).Where(group => group.SumQuantities > 41).Skip(0).Take(2).ToList();
                groups.ForEach(group =>
                {
                    Console.WriteLine("Category Id: " + group.Key);
                    Console.WriteLine("Count Product: " + group.CountProduct);
                    Console.WriteLine("Sum Quantities: " + group.SumQuantities);
                    Console.WriteLine("Min Price: " + group.MinPrice);
                    Console.WriteLine("Max Price: " + group.MaxPrice);
                    Console.WriteLine("Avg Price: " + group.AvgPrice);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Category Id: 2
Count Product: 2
Sum Quantities: 42
Min Price: 4
Max Price: 15
Avg Price: 9.5
---------------------
Category Id: 3
Count Product: 3
Sum Quantities: 63
Min Price: 17
Max Price: 43
Avg Price: 26.333333333333332
---------------------

You can use combination of Having and Skip method and Take method and OrderBy method as below:

using EntityFrameworkCore_ConsoleApp.Models;

namespace EntityFrameworkCore_ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var databaseContext = new DatabaseContext())
            {
                var groups = databaseContext.Products.GroupBy(product => product.CategoryId).Select(group => new
                {
                    group.Key,
                    CountProduct = group.Count(),
                    SumQuantities = group.Sum(productOfGroup => productOfGroup.Quantity),
                    MinPrice = group.Min(productOfGroup => productOfGroup.Price),
                    MaxPrice = group.Max(productOfGroup => productOfGroup.Price),
                    AvgPrice = group.Average(productOfGroup => productOfGroup.Price)
                }).Where(group => group.SumQuantities > 41).Skip(0).Take(2).OrderByDescending(group => group.SumQuantities).ToList();
                groups.ForEach(group =>
                {
                    Console.WriteLine("Category Id: " + group.Key);
                    Console.WriteLine("Count Product: " + group.CountProduct);
                    Console.WriteLine("Sum Quantities: " + group.SumQuantities);
                    Console.WriteLine("Min Price: " + group.MinPrice);
                    Console.WriteLine("Max Price: " + group.MaxPrice);
                    Console.WriteLine("Avg Price: " + group.AvgPrice);
                    Console.WriteLine("---------------------");
                });
            }
        }
    }
}
Category Id: 3
Count Product: 3
Sum Quantities: 63
Min Price: 17
Max Price: 43
Avg Price: 26.333333333333332
---------------------
Category Id: 2
Count Product: 2
Sum Quantities: 42
Min Price: 4
Max Price: 15
Avg Price: 9.5
---------------------