GroupBy Method in LINQ MeThods in Entity Framework Core

Use GroupBy method allows to group elements 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)
                }).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: 1
Count Product: 3
Sum Quantities: 41
Min Price: 5
Max Price: 20
Avg Price: 11.666666666666666
---------------------
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 GroupBy method and Where 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.Where(product => product.Status == true).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)
                }).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: 1
Count Product: 1
Sum Quantities: 15
Min Price: 10
Max Price: 10
Avg Price: 10
---------------------
Category Id: 2
Count Product: 1
Sum Quantities: 16
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------
Category Id: 3
Count Product: 2
Sum Quantities: 52
Min Price: 17
Max Price: 19
Avg Price: 18
---------------------

You can use combination of GroupBy method and Skip method and Where 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.Where(product => product.Status == true).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)
                }).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: 2
Count Product: 1
Sum Quantities: 16
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------
Category Id: 3
Count Product: 2
Sum Quantities: 52
Min Price: 17
Max Price: 19
Avg Price: 18
---------------------

You can use combination of GroupBy method and Skip method and Take method and Where 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.Where(product => product.Status == true).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)
                }).Skip(1).Take(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: 2
Count Product: 1
Sum Quantities: 16
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------

You can use combination of GroupBy method and Skip method and Take method and OrderBy method and Where 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.Where(product => product.Status == true).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)
                }).Skip(1).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: 2
Sum Quantities: 52
Min Price: 17
Max Price: 19
Avg Price: 18
---------------------
Category Id: 2
Count Product: 1
Sum Quantities: 16
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------