Converting ICollection<> to KeyedCollection<>

Posted on 2008-06-11
Last Modified: 2013-12-17
I have a generic ICollection (lets say List<object>).  I want to create a System.Collections.ObjectModel.KeyedCollection<string, object> from it.

How can I "convert" the ICollection to a KeyedCollection without iterating though each element?  Can I set the underlying collection in the KeyedCollection to my ICollection?
Question by:ChetOS82
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
LVL 18

Accepted Solution

jcoehoorn earned 500 total points
ID: 21759898
No.  You'll have to iterate.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21760668
it is not possible without iterating, even more when you have to specify the key for each element.
Who will "guess" the key for each element?
LVL 18

Expert Comment

ID: 21760746
@jaime: you have to inherit your own class from keyedcollection, and part of that is overriding an abstract method that will extract the key from the item.  
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 21760761
yeah, I know, I am trying to state that there is not a ready-made solution for this, for the reason explained.
LVL 18

Author Comment

ID: 21760926
As jcoehoorn said, there is no reason to specify the key, as it is determined from the value.

Anyway, after more research I agree with jcoehoorn.  The only way to add a value to a KeyedCollection is though the Add or SetItem methods.

I think the reason for this is because, internally, KeyedCollection is both an IList and an IDictionary and it can switch after a certain number of values have been added.  Even if there were a ICollection parameter in the constructor, it would still be iterating the collection.
LVL 18

Author Closing Comment

ID: 31466143

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET Calendar Control 5 52
VB.Net Data Class 1 45
Subtract dates in 6 33
Creating a Windows C++ program in visual studio 2015 5 29
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question