LINQ在一个语句中汇总嵌套组

时间:2011-10-04 02:25:07

标签: c# linq

我正在将一些代码转换为LINQ,同时探索LINQ可以完成的程度。

可以将以下代码压缩为单个LINQ查询或方法吗?

Dictionary<string, ItemPack> consolidated = new Dictionary<string, ItemPack>();
foreach (var product in Products)
{
    foreach (var smallpack in product.ItemPacks)
    {
        ItemPack bigpack;
        if (consolidated.TryGetValue(smallpack.ItemCode, out bigpack))
        {
            // the big pack quantity += quantity for making one product * the number of that product
            bigpack.Quantity += smallpack.Quantity * product.Quantity;

            // References: we make sure that the small pack is using the Item in the big pack.
            // otherwise there will be 2 occurance of the same Item
            smallpack.Item = bigpack.Item;
        }
        else
        {
            bigpack = new ItemPack(smallpack); // Copy constructor
            bigpack.Quantity = smallpack.Quantity * product.Quantity;
            consolidated.Add(smallpack.ItemCode, bigpack);
        }
    }
}
return consolidated;

在英语中,每个产品都由几个不同数量的项目组成。这些项目按项目代码分组,并打包成小包装。这些小包装作为单元产品一起运输。有许多不同的产品。单个项目可用于不同的产品。

我现在有一份产品清单和每件货物所需的数量。我想要一个LINQ语句来合并一个项目及其数量的平面列表。

我已经做到这一点,但看起来它不起作用:

var packsQuery = from product in Products
                 from smallpack in product.ItemPacks
                 select new {Item = smallpack.Item, Quantity = smallpack.Quantity * product.Quantity};

foreach (var pack in packsQuery)
{
    consolidated.Add(pack.Item.ItemCode, new ItemPack(pack.Item, pack.Quantity));
}

如果我先分组,那么我就无法选择其数量的项目。如果我先选择,那么我就失去了分组。鸡肉和鸡蛋的故事?

编辑: 实用注意事项:smallpack的类型为ItemPack,如下所示

public class ItemPack
{
     Item { get; } // The item in this pack, which *must* be a shared reference across all objects that uses this Item. So that change in Item properties are updated everywhere it is used. e.g. Price.
     ItemCode { get; } // The item code
     Quantity { get; } // The number of such Item in this pack.
}

2 个答案:

答案 0 :(得分:0)

    var query = (from product in Products
                from smallPack in product.ItemPacks
                select new
                {
                    ItemCode = smallPack.ItemCode,
                    Item = smallPack.Item,
                    Quantity = smallPack.Quantity * product.Quantity,
                })
                .GroupBy(p => p.ItemCode)
                .Select(p => new
                {
                    ItemCode = p.Key,
                    Item = p.FirstOrDefault(),
                    Quantity = p.Sum(x=>x.Quantity)
                })
                .ToDictionary(p=>p.ItemCode);

答案 1 :(得分:0)

感谢您让我朝着正确的方向前进。我设法计算出完整的查询语法版本:

var query = from product in Products
            from smallpack in product.ItemPacks
            select new {
                Item = smallpack.Item,
                Quantity = smallpack.Quantity * product.Quantity
            } into mediumpack
            group mediumpack by mediumpack.Item.ItemCode into bigpack
            select new {
                Item = bigpack.First().Item, // shared reference
                Quantity = bigpack.Sum(a => a.Quantity);
            }

query.ToDictionary(...);

是否有任何意见?