Deprecated: Creation of dynamic property Kirki_Toolkit::$modules is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/kirki.php on line 67

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Textarea::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Toggle::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Multicheck::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Number::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Text::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Multicheck::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Checkbox::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Switch::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Radio_Image::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Deprecated: Creation of dynamic property Kirki_Field_Custom::$label is deprecated in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php on line 291

Warning: Cannot modify header information - headers already sent by (output started at /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-content/themes/hitmag/inc/kirki/core/class-kirki-field.php:291) in /home/rh00k03md44z/public_html/new.octopuscodes.com/wp-includes/feed-rss2.php on line 8
Group By in entity framework Archives - OctopusCodes https://octopuscodes.com/tag/group-by-in-entity-framework/ Sun, 30 Apr 2023 04:59:21 +0000 en-US hourly 1 https://wordpress.org/?v=6.9 https://octopuscodes.com/wp-content/uploads/2022/12/cropped-logo-80x80-1-32x32.png Group By in entity framework Archives - OctopusCodes https://octopuscodes.com/tag/group-by-in-entity-framework/ 32 32 GroupBy in LINQ Query in Entity Framework Core https://octopuscodes.com/net-core-tutorial/entity-framework-core-tutorial/groupby-in-linq-query-in-entity-framework-core/ Sun, 30 Apr 2023 04:39:57 +0000 https://octopuscodes.com/?p=2102 Use GroupBy 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 …

The post GroupBy in LINQ Query in Entity Framework Core appeared first on OctopusCodes.

]]>
Use GroupBy 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 = (from product in databaseContext.Products
                              group product by product.CategoryId into gr
                              select new
                              {
                                  gr.Key,
                                  CountProduct = gr.Count(),
                                  SumQuantities = gr.Sum(productOfGroup => productOfGroup.Price),
                                  MinPrice = gr.Min(productOfGroup => productOfGroup.Price),
                                  MaxPrice = gr.Max(productOfGroup => productOfGroup.Price),
                                  AvgPrice = gr.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: 35
Min Price: 5
Max Price: 20
Avg Price: 11.666666666666666
---------------------
Category Id: 2
Count Product: 2
Sum Quantities: 19
Min Price: 4
Max Price: 15
Avg Price: 9.5
---------------------
Category Id: 3
Count Product: 3
Sum Quantities: 79
Min Price: 17
Max Price: 43
Avg Price: 26.333333333333332
---------------------

You can use combination of GroupBy 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 groups = (from product in databaseContext.Products
                              where product.Status == true 
                              group product by product.CategoryId into gr
                              select new
                              {
                                  gr.Key,
                                  CountProduct = gr.Count(),
                                  SumQuantities = gr.Sum(productOfGroup => productOfGroup.Price),
                                  MinPrice = gr.Min(productOfGroup => productOfGroup.Price),
                                  MaxPrice = gr.Max(productOfGroup => productOfGroup.Price),
                                  AvgPrice = gr.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: 10
Min Price: 10
Max Price: 10
Avg Price: 10
---------------------
Category Id: 2
Count Product: 1
Sum Quantities: 15
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------
Category Id: 3
Count Product: 2
Sum Quantities: 36
Min Price: 17
Max Price: 19
Avg Price: 18
---------------------

You can use combination of GroupBy and Skip 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 groups = (from product in databaseContext.Products
                              where product.Status == true 
                              group product by product.CategoryId into gr
                              select new
                              {
                                  gr.Key,
                                  CountProduct = gr.Count(),
                                  SumQuantities = gr.Sum(productOfGroup => productOfGroup.Price),
                                  MinPrice = gr.Min(productOfGroup => productOfGroup.Price),
                                  MaxPrice = gr.Max(productOfGroup => productOfGroup.Price),
                                  AvgPrice = gr.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: 15
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------
Category Id: 3
Count Product: 2
Sum Quantities: 36
Min Price: 17
Max Price: 19
Avg Price: 18
---------------------

You can use combination of GroupBy and Skip and Take 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 groups = (from product in databaseContext.Products
                              where product.Status == true 
                              group product by product.CategoryId into gr
                              select new
                              {
                                  gr.Key,
                                  CountProduct = gr.Count(),
                                  SumQuantities = gr.Sum(productOfGroup => productOfGroup.Price),
                                  MinPrice = gr.Min(productOfGroup => productOfGroup.Price),
                                  MaxPrice = gr.Max(productOfGroup => productOfGroup.Price),
                                  AvgPrice = gr.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: 15
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------

You can use combination of GroupBy and Skip and Take and OrderBy 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 groups = (from product in databaseContext.Products
                              where product.Status == true
                              group product by product.CategoryId into gr
                              select new
                              {
                                  gr.Key,
                                  CountProduct = gr.Count(),
                                  SumQuantities = gr.Sum(productOfGroup => productOfGroup.Price),
                                  MinPrice = gr.Min(productOfGroup => productOfGroup.Price),
                                  MaxPrice = gr.Max(productOfGroup => productOfGroup.Price),
                                  AvgPrice = gr.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: 36
Min Price: 17
Max Price: 19
Avg Price: 18
---------------------
Category Id: 2
Count Product: 1
Sum Quantities: 15
Min Price: 15
Max Price: 15
Avg Price: 15
---------------------

The post GroupBy in LINQ Query in Entity Framework Core appeared first on OctopusCodes.

]]>
GroupBy Method in LINQ MeThods in Entity Framework Core https://octopuscodes.com/net-core-tutorial/entity-framework-core-tutorial/groupby-method-in-linq-methods-in-entity-framework-core/ Sat, 15 Apr 2023 02:21:31 +0000 https://octopuscodes.com/?p=1745 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 …

The post GroupBy Method in LINQ MeThods in Entity Framework Core appeared first on OctopusCodes.

]]>
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
---------------------

The post GroupBy Method in LINQ MeThods in Entity Framework Core appeared first on OctopusCodes.

]]>