Solved

Converting ICollection<> to KeyedCollection<>

Posted on 2008-06-11
6
1,216 Views
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?
0
Comment
Question by:ChetOS82
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 21759898
No.  You'll have to iterate.
0
 
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?
0
 
LVL 18

Expert Comment

by:jcoehoorn
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.  
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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.
0
 
LVL 18

Author Comment

by:ChetOS82
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.
0
 
LVL 18

Author Closing Comment

by:ChetOS82
ID: 31466143
Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now