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

  • SEARCH
  • Home
  • 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 9121659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:54:45+00:00 2026-06-17T05:54:45+00:00

I am struggling with a double grouping using C#/LINQ on data similar in shape

  • 0

I am struggling with a double grouping using C#/LINQ on data similar in shape to the following example. I’m starting with a list of TickItems coming from the data layer, and I have now got it shaped as such:

TickItem {
ItemName: string;
QtyNeeded: int;
QtyFulfilled: int;
Category: string;
}

List<TickItem> items = new List<TickItem>();
items.Add("apple", 1, 0, "food");
items.Add("orange", 1, 1, "food");
items.Add("orange", 1, 0, "food");
items.Add("bicycle", 1, 1, "vehicle");
items.Add("apple", 1, 1, "food");
items.Add("apple", 1, 0, "food");
items.Add("car", 1, 1, "vehicle");
items.Add("truck", 1, 0, "vehicle");
items.Add("banana", 1, 0, "food");

I need to group this data by Category, with the sum of each numeric column in the end result. In the end, it should be shaped like this:

{ "food":    { "apple"  : 3, 1 },
             { "banana" : 1, 0 },
             { "orange" : 2, 1 } },
{ "vehicle": { "bicycle": 1, 1 },
             { "car"    : 1, 1 },
             { "truck"  : 1, 0} }

I have been able to do each of the groupings individually (group by ItemName and group by Category), but I have not been able to perform both groupings in a single LINQ statement. My code so far:

var groupListItemName = things.GroupBy(tiλ => tiλ.ItemName).ToList();
var groupListCategory = things.GroupBy(tiλ => tiλ.Category).ToList();

Can anyone help?

[edit: I can use either method or query syntax, whichever is easier to visualize the process with]

  • 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-06-17T05:54:46+00:00Added an answer on June 17, 2026 at 5:54 am
    var query = from i in items
                group i by i.Category into categoryGroup
                select new
                {
                   Category = categoryGroup.Key,
                   Items = categoryGroup.GroupBy(g => g.ItemName)
                                        .Select(g => new
                                        {
                                            ItemName = g.Key,
                                            QtyNeeded = g.Sum(x => x.QtyNeeded),
                                            QtyFulfilled = g.Sum(x => x.QtyFulfilled)
                                        }).ToList()
                };
    

    This query will return sequence of anonymous objects representing items grouped by category. Each category object will have list of anonymous objects, which will contain totals for each item name.

    foreach(var group in query)
    {
        // group.Category
        foreach(var item in group.Items)
        {
            // item.ItemName
            // item.QtyNeeded
            // item.QtyFulfilled 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was struggling with part of my code today to read some data from
Struggling to get any list function to work. I've been fine with _show and
struggling a bit to get the following to work : I'm trying to merge
Im struggling with Valumx exellent Fileupload. Ive been using it for a couple of
Im struggling with this one. I have a list of items and on a
I am building a list of agent id's in my database with the following
I'm struggling with the following problem and I can't find an acceptable way to
http://www.clker.com/clipart-green-right-double-arrows-set.html I am struggling to apply 9patch to an image like the one above
I am manually converting code from Java to C# and struggling with (what I
I'm currently struggling with the following code, the intent of which is to implement

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.