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 7648199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:39:18+00:00 2026-05-31T10:39:18+00:00

For example, how can I group the following records by GroupId using LINQ, and

  • 0

For example, how can I group the following records by GroupId using LINQ, and sum all other columns in each group? (thus merging all rows in each group into one)

var list = new List<Foo>()
{ 
    new Foo() { GroupId = 0, ValueA = 10, ValueB = 100 },
    new Foo() { GroupId = 1, ValueA = 30, ValueB = 700 },
    new Foo() { GroupId = 1, ValueA = 40, ValueB = 500 },
    new Foo() { GroupId = 2, ValueA = 80, ValueB = 300 },
    new Foo() { GroupId = 2, ValueA = 20, ValueB = 200 },
    new Foo() { GroupId = 2, ValueA = 20, ValueB = 200 }
};

Expected result is :

| GroupId | ValueA | ValueB |
|---------|--------|--------|
|    0    |   10   |   100  |
|    1    |   70   |   1200 |
|    2    |   120  |   700  |
  • 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-31T10:39:20+00:00Added an answer on May 31, 2026 at 10:39 am
    list.GroupBy(i => i.GroupId)
        .Select(g => new { GroupId = g.Key, 
                           ValueA = g.Sum(i => i.ValueA), 
                           ValueB = g.Sum(i => i.ValueB)});
    

    or just for fun you can do it within one GroupBy call using its overload:

    list.GroupBy(i => i.GroupId,
           (key, groupedItems) => new {
                                          GroupId = key,
                                          ValueA = groupedItems.Sum(i => i.ValueA),
                                          ValueB = groupedItems.Sum(i => i.ValueB),
                                      });
    

    or you can use Aggregate to avoid iterating each group many times:

    list.GroupBy(i => i.GroupId)
        .Select(g => g.Aggregate((i1, i2) => new Foo{ GroupId = i1.GroupId,
                                                      ValueA = i1.ValueA + i2.ValueA,
                                                      ValueB = i1.ValueB + i2.ValueB,
                                                    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following example i can create an object dynamically via a string; however,
I have the following example data in javascript: var variations = [ {group: 1,
I have a list of records with the following structure: (Simplified example!) class Rate
I know LINQ can group by taking advantage of anonymous types, so I though
I can group by seconds or minutes with something like the following: SELECT datepart(minute,
For example can i have something like this: public static void SAMENAME (sameparameter n)
For example can I have class Dog { int legs; Bone chewy; Bone squeeky;
For example DbVisualizer can be used to connect to a DB and create nice
Every example I can find is in C++, but I'm trying to keep my
I meam for example I can get var collection = from c in el.Descendants(current_conditions)

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.