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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:36:50+00:00 2026-05-26T07:36:50+00:00

I have two collections that I want to intersect, and perform a sum operation

  • 0

I have two collections that I want to intersect, and perform a sum operation on matching elements.

For example the collections are (in pseudo code):

col1 = { {"A", 5}, {"B", 3}, {"C", 2} }
col2 = { {"B", 1}, {"C", 8}, {"D", 6} }

and the desired result is:

intersection = { {"B", 4}, {"C", 10} }

I know how to use an IEqualityComparer to match the elements on their name, but how to sum the values while doing the intersection?

EDIT:

The starting collections haven’t two items with the same name.

  • 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-26T07:36:51+00:00Added an answer on May 26, 2026 at 7:36 am

    Let’s say your input data looks like this:

    IEnumerable<Tuple<string, int>> firstSequence = ..., secondSequence = ...;
    

    If the strings are unique in each sequence (i.e there can be no more than a single {“A”, XXX} in either sequence) you can join like this:

    var query = from tuple1 in firstSequence
                join tuple2 in secondSequence on tuple1.Item1 equals tuple2.Item1
                select Tuple.Create(tuple1.Item1, tuple1.Item2 + tuple2.Item2);
    

    You might also want to consider using a group by, which would be more appropriate if this uniqueness doesn’t hold:

    var query = from tuple in firstSequence.Concat(secondSequence)
                group tuple.Item2 by tuple.Item1 into g
                select Tuple.Create(g.Key, g.Sum());
    

    If neither is what you want, please clarify your requirements more precisely.

    EDIT: After your clarification that these are dictionaries – your existing solution is perfectly fine. Here’s another alternative with join:

    var joined = from kvp1 in dict1
                 join kvp2 in dict2 on kvp1.Key equals kvp2.Key
                 select new { kvp1.Key, Value = kvp1.Value + kvp2.Value };
    
    var result = joined.ToDictionary(t => t.Key, t => t.Value);
    

    or in fluent syntax:

    var result = dict1.Join(dict2,
                            kvp => kvp.Key,
                            kvp => kvp.Key,
                            (kvp1, kvp2) => new { kvp1.Key, Value = kvp1.Value + kvp2.Value })
                      .ToDictionary(a => a.Key, a => a.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two collections of my own reference-type objects that I wrote my own
I have two ObservableCollection lists, that i want to unite. My naive approach was
I have two IEnumerable collections that I would like to union. One selects news
I am trying to create multiple records with two collections that i have View
I have a testing scenario where I want to check if two collections are
Let's say you have two collections of integers: IEnumerable<int> col1=new List<int> {2,3,3,5,7,11,11,11,13}; IEnumerable<int> col2=new
I have two generic collections in my code, EventableCollection and EventableSortedList - which are
I have two collections which have property Email in both collections. I need to
I have a class which has two HashSet<String> collections as private members. Other classes
I have two Collection objects, I want to associate each object of these two

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.