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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:12:40+00:00 2026-06-18T03:12:40+00:00

I have IDictionary<string,object> d1; Dictionary<string, object> d2; I need to remove from d1 all

  • 0

I have

IDictionary<string,object> d1;
Dictionary<string, object> d2;

I need to remove from d1 all entries that are not in d2.

I know I can do this with a for loop etc but that’s so last century; I want to do it right.

I got to

   d1.Where(x => {return d2.ContainsKey(x.key);});

but dont know what to do next

  • 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-06-18T03:12:41+00:00Added an answer on June 18, 2026 at 3:12 am

    LINQ isn’t designed to modify existing elements – but you could always create a new dictionary. For example:

    d1 = d1.Where(x => d2.ContainsKey(x.Key))
           .ToDictionary(x => x.Key, x => x.Value);
    

    Or:

    d1 = d1.Keys.Intersect(d2.Keys)
           .ToDictionary(key => x.Key, key => d1[key]);
    

    As others have said, if you’re more keen on doing a Remove operation, I’d just loop. For example:

    foreach (var key in d1.Keys.Except(d2.Keys).ToList())
    {
        d1.Remove(key);
    }
    

    (I’m not sure why you used a statement lambda in your sample code, by the way.)

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

Sidebar

Related Questions

I have a simple Dictionary(of String, Object) that I need to iterate through and
I need a Dictionary<string,object> which is produced from a DataRow. I currently have something
I have a string data which I need to parse into a dictionary object.
I have a dictionary object IDictionary<string, string> which only has the following items: Item1,
I have a possible implementation scenario where I need a dictionary object that will
I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>>
I have a bunch of named value parameters in a Dictionary<string, object> , which
I have a dictionary public static IDictionary<string, IList<string>> checksCollection = new Dictionary<string, IList<string>>(); I
I have a dictionary as follows: IDictionary<string, string> dict; How to create an enumerator

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.