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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:08:11+00:00 2026-05-19T13:08:11+00:00

What is a LINQ way of aggregating column values in a table, where each

  • 0

What is a LINQ way of aggregating column values in a table, where each row is a Dictionary<ColumnT, ValueT>?

The data table is of form

   2010 | 2011
A    11 |   12
B    21 |   22

This is represented as a list of dictionaries:

var A = new Dictionary<String, int>();
A.Add("2010", 11);
A.Add("2011"  12),
var B = new Dictionary<String, int>();
A.Add("2010", 21);
A.Add("2011"  22),
var table = List<Dictionary<String,int>>() { A, B };

How would one produce a totals row that aggregates all columns from other rows? The totals row would also be a Dictionary<String, int> like the other rows.

The old style way is:

var totalsRow = new Dictionary<String, int>();
foreach(Dictionary<String, int> row in table)
{
    foreach(var cell in row)
    {
       // Get running sum (add if not present)
       int TValue= 0;
       if (!totalsRow.TryGetValue(cell.Key, out cellValue))
       {
          totalsRow.Add(cell.Key, 0);
       }

       // Increment using an aggregation function (e.g. sum)
       totalsRow[cell.Key] = AggregationFunc(totalsRow[cell.Key], cellValue);
    }
}

Is there a more succinct, LINQ way of doing this?

If not, what is the most LINQ-like way to define an extension method (on Dictionary) to wrap the above code?

Clarification: AggregationFunc is any function that takes the running total and next item value and produces the new running total, e.g.

int Sum(int sum, int nextValue) { return sum + nextValue; }
  • 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-19T13:08:12+00:00Added an answer on May 19, 2026 at 1:08 pm

    I assume your last line is actually:

    totalsRow[cell.Key] = AggregationFunc(runningValue, cell.Value);
    

    because otherwise, you’re not using the integer-values of the embedded dictionaries anywhere.


    You could do:

    var totalsRow = table.SelectMany(row => row)
                         .GroupBy(kvp => kvp.Key)
                         .ToDictionary(group => group.Key, 
                                       group => group.Sum(kvp => kvp.Value));
    

    If you want to keep the AggregationFunc, replace the second argument to ToDictionary with:

    group => group.Aggregate(0, 
             (runningValue, nextKvp) => AggregationFunc(runningValue, nextKvp.Value))
    

    The idea is to:

    1. First flatten the list of dictionaries into a sequence of key-value pairs.
    2. Group the key-value pairs by key.
    3. Turn the groups of key-value pairs into a dictionary with:
      • The “key” being the group‘s key, i.e. the key shared by all the pairs in the group.
      • The “value” being the aggregation of the individual values of all the pairs in the group.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most efficient way of converting a single column linq query to
Is there a way to write a single LINQ expression to get the same
Anybody know of a way to batch NHibernate queries using NHibernate.Linq like you can
this is the way i used to save record with linq: (my Q is
is there any way to write this foreach in linq or another better way,
I am using LINQ and I was wondering what is the best way to
Is there a way to duplicate a db record with linq to sql in
A good thing in LINQ to SQL was a fast and reliable way to
What's the best way to do this with Linq? I have a List that
I was wondering what the correct-way is when using a Linq to Sql-model in

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.