多重分组

时间:2018-11-10 07:37:06

标签: c# linq

我有一个任务-“将所有产品按类别分组-在内部-按库存的可用性,在最后一组在内部,按价​​格分组。”

数据:

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 }
};

这是我想出的解决方案,但我无法比较库存和价格。

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 我希望库存和价格在同一行

预期结果:

enter image description here

该任务的整个复杂性是,从一开始就需要按类别,然后按UnitsInStock和UnitPrice进行分组。并将所有这些输出分组

1 个答案:

答案 0 :(得分:0)

我相信这会给您您想要的东西。

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}");
    }
}

那给了我

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
相关问题