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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:10:57+00:00 2026-05-25T22:10:57+00:00

I am learning C# and needed to merge two dictionaries so I could add

  • 0

I am learning C# and needed to merge two dictionaries so I could add values found in both.

The first dictionary is payePlusNssf that holds a value for each key (key represents employee ID). So far I have employees 1,2,3,4 and 5

Dictionary<int, decimal> payePlusNssf = new Dictionary<int, decimal>();

paye.ToList().ForEach(x =>
{
    var deductNSSF = x.Value + nssfAmount;

    payePlusNssf.Add(x.Key, deductNSSF);
});

The 2nd dictionary is nhifRatesDictionary that holds rates to be added to each value per employee in the first dictionary.

Dictionary<int, IEnumerable<NHIFRates>> nhifRatesDictionary =
   new Dictionary<int, IEnumerable<NHIFRates>>();

basicPayDictionary.ToList().ForEach(x =>
{
    List<NHIFRates> nhifValueList = new List<NHIFRates>();

    // Using Employee basic pay
    decimal basicPay = x.Value;

    bool foundflag = false;

    foreach (var item in nhifBracketList)
    {
        if (basicPay >= item.Min && basicPay <= item.Max)
        {
            nhifValueList.Add(new NHIFRates { Rate = item.Rate });

            foundflag = true;
            break;
        }                       
    }

    if (!foundflag)
    {
        nhifValueList.Add(new NHIFRates { Rate = 0m });
    }

    nhifRatesDictionary.Add(x.Key, nhifValueList);
});

struct NHIFRates
{
    public decimal Rate { get; set; }
}

In summary I need this after merging and adding:

Dict 1          Dict 2          Result Dict 3
key  value      key  rate       key  value
1     500       1     80        1    580
2    1000       2     100       2   1100
3    2000       3     220       3   2220
4     800       4     300       4   1100
5    1000       5     100       5   1100

How do I achieve this? I have looked at past similar problems on this site but have not been very helpful to me.

  • 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-25T22:10:58+00:00Added an answer on May 25, 2026 at 10:10 pm

    Not tested but try:

    payePlusNssf.ToDictionary(
                v => v.Key, 
                v => v.Value + nhifRatesDictionary[v.Key].Sum(nhifr => nhifr.Rate)
                             );
    

    This assumes that since the value of nhifRatesDictionary is IEnumreable you want to sum over all the values in the enumerable. This should also work if the IEnumerable is empty. If you know that there is exactly one value for each key then you can use:

    payePlusNssf.ToDictionary(
                v => v.Key, 
                v => v.Value + nhifRatesDictionary[v.Key].Single(nhifr => nhifr.Rate)
                             );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning Scala currently and needed to invert a Map to do some inverted value->key
Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring
Learning a lot in my few years of programming that the best projects are
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
Learning from my broad question here ... I was wondering how I could go
Im learning C++ and I ran into something strange that I couldn't find any
I just started learning portlet and got stuck in the first place. I have
I'm learning Javascript via an online tutorial, but nowhere on that website or any
I've am learning about Python-C extensions and am puzzled as to why methods that
I hear from everywhere that NHibernate 3 learning curve is very huge. It is

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.