Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:12:27+00:00 2026-05-26T02:12:27+00:00

I am converting some code to LINQ, at the same time exploring to what

  • 0

I am converting some code to LINQ, at the same time exploring to what extent LINQ can accomplish.

Can the following code be condensed into a single LINQ query or method?

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;

In English, each product is made up of several items of different quantities. These items are grouped by item code and are packs into smallpacks. These smallpacks are shipped together as a unit product. There are many different products. A single item can be used in different product.

I now have a list of products and the quantity required for each for shipment. I want a LINQ statement to consolidate a flat list of items and their quantities.

I have gotten this far, but it looks like it does not work:

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

If I group first, then I cannot select item for its quantity. If I select first, then I lose the grouping. Chicken and egg story?

EDIT:
Useful note: smallpack is of type ItemPack which looks like this

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.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:12:28+00:00Added an answer on May 26, 2026 at 2:12 am
        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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into a problem whilst converting some C code to PHP, specifically in
I'm converting some C# code into VB.Net and was stuck on second half of
I'm converting some C code to Delphi. Can someone please explain to me what
I am converting some php code into a Ruby script and I am attempting
I'm just beginning in scala and I'm converting some java code into scala and
I am converting some Java code to C#. This code is using getGlyphOutline from
Searching for some sample code for converting a point in WGS84 coordinate system to
I've got some C# code that I'm converting to Objective-C. In C# I would
I am manually converting code from Java (1.6) to C# and finding some difficulty
Recently I attended a lecture concerning some design patterns: The following code had been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.