0

I have a task - "Group all products into categories, inside - by availability in stock, inside the last group, group by price."

Data:

var productList =
new List<Product> {
    new Product { ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 },
    new Product { ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 },
    new Product { ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 },
    new Product { ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 },
    new Product { ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 },
    new Product { ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 },
    new Product { ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 },
    new Product { ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 },
    new Product { ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 },
    new Product { ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 },
    new Product { ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 },
    new Product { ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 },
    new Product { ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 },
    new Product { ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 },
    new Product { ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 },
    new Product { ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 },
    new Product { ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 },
    new Product { ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57 }
};

Here is the solution that I came up with, but I can not compare stock and price.

var category = productList
                .GroupBy(g => g.Category)
                .Select(s => new
                    {
                        Category = s.Key,
                        UnitsInStock = s.Select(s2 => s2.UnitsInStock),
                        UnitPrice = s.Select(s2 => s2.UnitPrice)
                    });

var unitsInStock = category.GroupBy(g=> new { g.Category, g.UnitsInStock})
                    .Select(s => new
                        {
                            UnitsInStock = s.Key.UnitsInStock,
                            Category = s.Key.Category,
                            UnitPrice = s.Select(s2 => s2.UnitPrice)
                        });

var unitPrice = unitsInStock.GroupBy(g => new { g.Category, g.UnitsInStock, g.UnitPrice})
                    .Select(s => new
                        {
                            UnitPrice = s.Key.UnitPrice,
                            Category = s.Key.Category,
                            UnitsInStock = s.Key.UnitsInStock
                        });

foreach (var categ in unitPrice)
{
    Console.WriteLine($"Category: {categ.Category}");

    foreach (var stock in categ.UnitsInStock)
    {
        Console.WriteLine($"UnitsInStock: {stock}");
    }
    foreach (var price in categ.UnitPrice.SelectMany(s => s))
    {
        Console.WriteLine($"UnitPrice: {price}");
    }
}

enter image description here I want stock and price to be in one line

Expected result:

enter image description here

The entire complexity of the task is that it is necessary from the beginning from grouping by Category, then by UnitsInStock, then UnitPrice. And all this output is grouped

Tibomso
  • 363
  • 1
  • 10

1 Answers1

0

I believe this gives you what you want.

var category =
    productList
        .GroupBy(g => g.Category)
        .Select(s => new
        {
            Category = s.Key,
            Units = s.Select(s2 => new { s2.UnitsInStock, s2.UnitPrice })
        });


foreach (var categ in category)
{
    Console.WriteLine($"Category: {categ.Category}");
    foreach (var unit in categ.Units)
    {
        Console.WriteLine($"UnitsInStock: {unit.UnitsInStock}; UnitPrice: {unit.UnitPrice}");
    }
}

That gives me:

Category: Beverages
UnitsInStock: 39; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 19.0000
UnitsInStock: 20; UnitPrice: 4.5000
UnitsInStock: 111; UnitPrice: 14.0000
UnitsInStock: 20; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 263.5000
UnitsInStock: 69; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 46.0000
UnitsInStock: 52; UnitPrice: 14.0000
UnitsInStock: 15; UnitPrice: 15.0000
UnitsInStock: 125; UnitPrice: 7.7500
UnitsInStock: 57; UnitPrice: 18.0000
Category: Meat/Poultry
UnitsInStock: 29; UnitPrice: 97.0000
UnitsInStock: 0; UnitPrice: 39.0000
UnitsInStock: 0; UnitPrice: 123.7900
UnitsInStock: 0; UnitPrice: 32.8000
UnitsInStock: 21; UnitPrice: 7.4500
UnitsInStock: 115; UnitPrice: 24.0000
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Do I understand correctly that my groups unitsInStock and unitPrice are not needed? – Tibomso Nov 10 '18 at 07:58
  • That appears to be right based on your desired output. – Enigmativity Nov 10 '18 at 08:00
  • Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you – Tibomso Nov 10 '18 at 08:06
  • What do you mean by "pay attention to the task itself"? – Enigmativity Nov 10 '18 at 08:11
  • I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done? – Tibomso Nov 10 '18 at 08:13
  • The entire complexity of the task is that it is necessary from the beginning from grouping by Category, then by UnitsInStock, then UnitPrice. And all this output is grouped – Tibomso Nov 10 '18 at 08:15
  • @Enigmativity my understanding is that OP wants a list grouped by Category _AND_ a list grouped by Stock _AND_ a list grouped by UnitPrice, such that you can find all items with a stock of 0 for example by searching the 'UnitsInStock' list. Though I think this is a mis-interpretation of his *actual task* on his part, which is what your answer solves. – Enfyve Nov 10 '18 at 08:28
  • @Enfyve, First of all, we group by “Category”, then inside the result we group by “UnitInStock”, then in the inside we group the result by “UnitPrice”. I agree that the formulation of the problem is delusional, but what is there :( – Tibomso Nov 10 '18 at 08:36
  • @Enfyve, There is no And in the grouping, just by the next grouping 1-> 2-> 3 – Tibomso Nov 10 '18 at 08:37
  • `var unitPrice = unitsInStock.GroupBy(g => new { g.Category, g.UnitsInStock, g.UnitPrice }) .Select(s => new { Category = s.Key.Category, UnitsInStock = s.Key.UnitsInStock.Select(s2=>s2), UnitPrice = s.Key.UnitPrice.SelectMany(s2=>s2) }); var result2 = unitPrice.Select( s => new { Category = s.Category, Units = new { s.UnitsInStock, s.UnitPrice } });` - You can tell me how to display in this case the units, there inside the collection – Tibomso Nov 10 '18 at 08:45