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

The Archive Base Latest Questions

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

I have a List<string> AllowedList and a Dictionary<string,List<string>> MyDictionary . For each key in

  • 0

I have a List<string> AllowedList and a Dictionary<string,List<string>> MyDictionary.

For each key in the dictionary, I want to check if it’s on the AllowedList, if not I want to delete the key and value from the dictionary.

I intuitively tried this which seems to be what I wanted:

foreach (string key in MyDictionary.Keys)
{
    if (!AllowedList.Contains(key)) MyDictionary.Remove(key);
}

However, I encountered an InvalidOperationException:

Collection was modified; enumeration operation may not execute.

I’m sure there’s probably a simple way around this, but I don’t immediately see it.

  • 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-12T12:53:41+00:00Added an answer on June 12, 2026 at 12:53 pm

    You could use Enumerable.Except to find the keys that are not in the dictionary:

    foreach (var key in MyDictionary.Keys.Except(AllowedList).ToList())
        MyDictionary.Remove(key);
    

    The ToList() creates a new list of the set difference and prevents the exception.

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

Sidebar

Related Questions

I have List[(String,String)] and I need to sort them by the 2nd value and
I have a list of string values that I want add to a hashtable
I have a list of string like: s = [(abc,bcd,cde),(123,3r4,32f)] Now I want to
I have a list: List(Of String) I want to filter it with LINQ. I
I have a list of string goal0=1234.4334abc12423423 goal1=-234234 asdfsdf I want to extract the
Hi all I have list of type string with some items. i want to
let undefined = [string; ; string; boolean;;innermost] I have a list and I want
I have list of string which I want to add as option in select
I have List<Dictionary<string, string>> object with some datas in it. /* Values in the
lets say that we have: string list[]= {12.34.56.78,55.34.5,23.44.5} I want the user to enter

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.