Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ在一条语句中使用嵌套组_C#_Linq - Fatal编程技术网

C# LINQ在一条语句中使用嵌套组

C# LINQ在一条语句中使用嵌套组,c#,linq,C#,Linq,我正在将一些代码转换为LINQ,同时探索LINQ可以达到什么程度 下面的代码可以压缩成单个LINQ查询或方法吗 Dictionary<string, ItemPack> consolidated = new Dictionary<string, ItemPack>(); foreach (var product in Products) { foreach (var smallpack in product.ItemPacks) { Ite

我正在将一些代码转换为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;
如果我先分组,则无法为其数量选择项目。如果先选择,则会丢失分组。鸡和蛋的故事

编辑: 有用说明: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.
}

谢谢你给我指出了正确的方向。我设法制定了完整的查询语法版本:

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(...);

对这是否合适有什么意见吗?

谢谢你给我指出了正确的方向。我设法制定了完整的查询语法版本:

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(...);

对这是否合适有什么意见吗?

是的,这和我的一样,所以很好。是的,这和我的一样,所以很好。
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(...);