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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:05:38+00:00 2026-05-17T21:05:38+00:00

I am comparing two dictionary(dic1 and dic2) with rule that get values from dic2

  • 0

I am comparing two dictionary(dic1 and dic2) with rule that get values from dic2 where key match but values does not match or key is missing in dic2.
Don’t need to iterate through dic2 for missing/different values in dic1.

Below code is working ok I would like to know is there any better way using .NET 2.0 (NO LINQ) .

if optimization is require which option is better?

Dictionary<string,List<foo>> dic1 = new Dictionary<string,List<foo>>();
Dictionary<string,List<foo>> dic2 = new Dictionary<string,List<foo>>();

dic1.add("1", new foo("a"));
dic1.add("2", new foo("b"));
dic1.add("3", new foo("c"));
dic1.add("3", new foo("c1"));
dic1.add("4", new foo("d"));

dic2.add("1", new foo("a"));
dic2.add("2", new foo("b1"));
dic2.add("3", new foo("c"));
dic2.add("3", new foo("c2"));

//I write code which allow duplicate key in dictionary 

Option 1

foreach (KeyValuePair<string, List<foo>> var in dic1)
{
    if (dic2.ContainsKey(var.Key))
    {
        List<foo> tempList = var.Value.FindAll(delegate(foo s)
        {
            return !dic2[var.Key].Contains(s);
        });
        result.AddRange(tempList);
    }
    else
    {
        result.Add(var.Value);
    }

}

Option 2

List<string> list1key = new List<string>(dic1.Keys);

list1key.ForEach(delegate(string key)
{
    if (dic2.ContainsKey(key))
    {
        List<foo> tempList = dic1[key].FindAll(delegate(foos)
           {
               return !dic2[key].Contains(s);
           });
        result.AddRange(tempList);
    }
    else
    {
        result.AddRange(dic1[key]);
    }
});
  • 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-17T21:05:38+00:00Added an answer on May 17, 2026 at 9:05 pm

    You can speed things up with either option if you use TryGetValue when accessing dic2, so you only have to do a key lookup once.

    Your first option looks simpler and possibly faster, i’d go with that.
    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When comparing two key-value dictionary sets in C#: set A and set B, what
I have some simple code that is comparing two float values to illustrate a
I have an android app. in that I am comparing two strings from the
From http://projecteuler.net/index.php?section=problems&id=99 Comparing two numbers written in index form like 2 11 and 3
Comparing two codes below,both do the same,but with slighty differences: ALTER procedure [dbo].[SP_USUARIOS_UPDATE] @usu_ds
hello i got a problem with comparing two char* variables that contains numbers for
i have an if statement comparing two NSStrings, one is user input from a
I've got some code that relies on comparing two NSIndexPaths and executing different code
When comparing two strings in c# for equality, what is the difference between InvariantCulture
I am comparing two almost identical folders which include hidden .svn folders which should

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.