Solved

Converting ICollection<> to KeyedCollection<>

Posted on 2008-06-11
6
1,219 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…

679 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