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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:26:43+00:00 2026-05-11T16:26:43+00:00

I have a Dictionary and a List of keys to remove from the dictionary.

  • 0

I have a Dictionary and a List of keys to remove from the dictionary. This is my implementation right now:

var keys = (from entry in etimes
            where Convert.ToInt64(entry.Value) < Convert.ToInt64(stime)
            select entry.Key).ToList();

foreach (var key in keys)
{
    etimes.Remove(key);
    count--;
}

Is there something I can do the eliminate the foreach loop?

  • 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-11T16:26:43+00:00Added an answer on May 11, 2026 at 4:26 pm
    var pruned = etimes.Where(entry => Convert.ToInt64(entry.Value) >= 
        Convert.ToInt64(stime)).ToDictionary(entry => entry.Key, 
            entry => entry.Value);
    

    This statement simply filters the dictionary using the LINQ Where function to select which items to keep rather than those to remove (which then requires further code, as you showed). The ToDictionary converts thwe IEnumerable<KeyValuePair<TKey, TValue>> to the desire Dictionary<TKey, TValue> type, and is at least quite simple, if not terribly elegant or efficient due to the need to specify the two lambda expressions to select the key and value from a KeyValuePair (and then creating a new dictionary). Saying this, I don’t really see it as a problem, especially if the dictionary is small and/or number of items being removed is large.

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

Sidebar

Related Questions

I have a dictionary and using .keys() to convert into a list of keys
Hi want to have a ordered dictionary with keys having list of values. from
I have a dictionary whose keys and values are updated from internet. This dictionary
I have a Dictionary<string, List<Order>> and I want to have the list of keys
I have a function, that returns the next higher value of a Dictionary-Keys-List compared
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I have a dictionary, user_dict, and create a list for each user value, this
I have a list which contains a dictionary of ExpandoObjects. Im binding this to
I have a dictionary d (and a seperate sorted list of keys, keys ).
Suppose I have a method to return a list of keys in a dictionary,

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.