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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:16:29+00:00 2026-05-28T15:16:29+00:00

I want to be able to look values up by a key in same

  • 0

I want to be able to look values up by a key in same cases, and in other cases iterate through the collection.

Previously I was doing this:

public class Company
{
  public string Name { get; set; }
  public ICollection<Department> Departments { get; set; }

}

public class Department
{
  public string Name       { get; set; } //display name
  public string UniqueName { get; set; } //like a code value
  public bool   IsSelected { get; set; }
}

foreach(var d in someCompany.Departments) ...

var departments = someCompany.Departments.Select( d => some projection stuff ...

Now I have several places I also need to iterate through some other collection like checkbox lists with matching strings for the UniqueName, so for it to be efficient I changed the declaration of Departments to an IDictionary, but this makes the other use cases more cumbersome, always requiring me to drill into a Value or Values. So now Departments property is no longer a collection of Department classes, but instead a collection of KeyValue pairs.

foreach(ListItem item in someCheckBoxes.Items)
{
  someCompany.Departments[item.Value].Selected = true;
}

foreach(var d in someCompany.Departments.Values) ...

var departments = someCompany.Departments.Values.Select( d => some projection stuff ...

I also don’t care for having to convert Lists to IDictionary or add a KeyValue whenever I initialize these lists.

Ideally I would have a collection that behaves like an ICollection but also has an index operator and Contains a function which internally access a dictionary.

OR I would have two properties like so, that stay in sync:

public Company(string uniqueName, string name, ICollection<Department> departments)
{
  Name = name;
  UniqueName = uniqueName;
  DepartmentsByUniqueName = departments.ToDictionary<Department, string>(p => p.UniqueName);
}

public IDictionary<string,Department> DepartmentsByUniqueName { get; set;}

public ICollection<Department> Departments { get { return DepartmentsByUniqueName.Values; } }

public void AddDepartment(Department department)
{ 
  DepartmentsByUniqueName.Add(department,department.UniqueName)
}

The problem here is that someone could get the Values collection via the Departments property and add/remove items to it, not reallizing they really need to be added to the dictionary instead.(implementing set doesn’t solve this because they get the cllection via the get and then can add items to it.)

Trying not to reinvent too much code, but inheriting from Dictionary automatically makes it inherit IEnumerable. Even if I do an explicit implementation of the interface, problems could occur if the user has cast it to that base interface.

So essentially I want a class that implements IEnumerable but also has a Contains and [] operators which utilize a dictionary internally for efficiency.

OR be able to create addition DepartmentByUniqueName dictionary property which stays in sync with the Departments collection.

  • 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-28T15:16:30+00:00Added an answer on May 28, 2026 at 3:16 pm

    What you may want to do is to subclass KeyedCollection and define it thus:

    public class DepartmentCollection : KeyedCollection<String, Department> {
        protected override String GetKeyForItem(Department item)
        {
            // EDIT: For your use case, this should work
            return item.UniqueName;
        }
    }
    

    And use it in your company class as the Departments property:

    public class Company
    {
      public string Name { get; set; }
      public DepartmentCollection Departments { get; set; }
    }
    

    The KeyedCollection can then be used either by name or by index:

    var departments = new DepartmentCollection();
    departments.Add(new Department( ... ));
    var accounting = departments["Accounting"];
    foreach (var department in departments) { .... } 
    var accountingExists = departments.Contains("accounting");
    // etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my application to look like this. I am able to get names
Want to be able to provide a search interface for a collection of objects
I want to be able to do this. MyInterface interface = new ServiceProxyHelper<ProxyType>(); Here's
Using Hashtable I want to have multiple objects mapped to the same key. E.g.
I'm using LaTeX and BibTeX for an article, and I want to able to
I want to be able to capture the exception that is thrown when a
I want to be able to do: For Each thing In things End For
I want to be able to play sound files in my program. Where should
I want to be able to make an HTTP call updating some select boxes
I want to be able to view a SQL Server 2005 Reporting Services report

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.