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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:03:33+00:00 2026-05-23T19:03:33+00:00

I am looking for a way that is as efficient as the Keys property

  • 0

I am looking for a way that is as efficient as the Keys property (of type KeyCollection) of a generic dictionary.

Using a Linq select statement would work, but it would iterate over the whole collection each time the keys are requested, while I believe the keys may already be stored internally.

Currently my GenericKeyedCollection class looks like this:

public class GenericKeyedCollection<TKey, TItem> : KeyedCollection<TKey, TItem> {
    private Func<TItem, TKey> getKeyFunc;

    protected override TKey GetKeyForItem(TItem item) {
        return getKeyFunc(item);
    }

    public GenericKeyedCollection(Func<TItem, TKey> getKeyFunc) {
        this.getKeyFunc = getKeyFunc;
    }

    public List<TKey> Keys {
        get {
            return this.Select(i => this.GetKeyForItem(i)).ToList();
        }
    }
}

Update: Thanks to your answers, I will therefore use the following property instead of iterating with Linq.

    public ICollection<TKey> Keys {
        get {
            if (this.Dictionary != null) {
                return this.Dictionary.Keys;
            }
            else {
                return new Collection<TKey>(this.Select(this.GetKeyForItem).ToArray());
            }
        }
    }
  • 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-23T19:03:34+00:00Added an answer on May 23, 2026 at 7:03 pm

    As per the documentation, the class has a property, Dictionary, so you can do this:

    var keys = collection.Dictionary.Keys;
    

    Note that there is a caveat, as described in the documentation. If you construct the collection with a threshold value for the dictionary, the dictionary will not be populated until at least that many values has been put into the collection.

    If this is not the case for your situation, ie. the dictionary is always good to go, the above code should do the trick.

    If not, then you either have to change your construction to avoid setting that threshold, or you will just have to loop and extract the key through the GetKeyForItem method.

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

Sidebar

Related Questions

I am looking for an efficient way to create a dynamic CrudViewModelBase<TConext, TEntity> that
I'm looking for any way that I can improve the selector performance of a
I'm looking for a way that I can keep a database in one single
I am looking for a way to ensure that my text field (JTextField, JFormattedTextField)
My problem is that I was looking for way to use both storyboard and
I am looking for a way to explain that it's unreasonable to sprinkle high-level
Im looking for a way to draw the OUTLINE of a circle that will
I'm looking for a way to poll different servers and check that SQL server
I looking for a way, specifically in PHP that I will be guaranteed to
I'm looking for a way to create an online form that will update an

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.