473,416 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Dictionary<> to List<>

I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.

1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a dictionary or list?
thanks,

-Andy
Mar 8 '07 #1
7 57507
How efficient is this?

Dictionary<EntityKey, Entitydictionary = new
Dictionary<EntityKey, Entity>();

// build the dictionary....

List<Entitylist = new List<Entity>(dictionary.Values);
"Andrew Robinson" <ne****@nospam.nospamwrote in message
news:eK*************@TK2MSFTNGP03.phx.gbl...
>I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.

1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a dictionary or list?
thanks,

-Andy

Mar 8 '07 #2
Hi Ronbinson
"Andrew Robinson" <ne****@nospam.nospamschrieb im Newsbeitrag
news:eK*************@TK2MSFTNGP03.phx.gbl...
>I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
Why this has to be one method? The caller needs to know, what the method
returns, I'd suppose. So why not having to methods? Maybe an overload?
>
1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
No there isn't.
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a dictionary or list?
Well, they both implement ICollection and IEnumerable. But they would have
different Elementtypes (KeyValuePair<k,vresp. v). And surely both
implement object ;-), but I don't think, that's what you want.

Christof
Mar 8 '07 #3
On 8 Mar, 16:43, "Andrew Robinson" <nem...@nospam.nospamwrote:
I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
Dictionary.Values seems to behave in the same way as the List object,
e.g.

Dictionary<int, stringdict = new Dictionary<int,
string>();
foreach (string s in dict.Values)
{
}

List<stringlist = new List<string>();
foreach (string s in list)
{
}

[I was about to post this and then spotted your own reply... If they
behave as ecpected, then I guess it does boil down to efficiency -
have you done some timings?]

Mar 8 '07 #4
Andrew Robinson wrote:
How efficient is this?

Dictionary<EntityKey, Entitydictionary = new
Dictionary<EntityKey, Entity>();

// build the dictionary....

List<Entitylist = new List<Entity>(dictionary.Values);

If it doesn't absolutely have to be a List<Entity>, you can use:

ValueCollection<Entitylist = dictionary.Values;

or:

ICollection<Entitylist = dictionary.Values;

or:

IEnumerable<Entitylist = dictionary.Values;
ValueCollection<Timplements both ICollection<Tand IEnumerable<T>.
If you want a list, the constructor of the list that takes the
collection will just copy the elements of the collection into a new
list. If Entity is a class, this means that it just copies the references.

--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #5
thanks,

-A

"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ**************@TK2MSFTNGP03.phx.gbl...
Andrew Robinson wrote:
>How efficient is this?

Dictionary<EntityKey, Entitydictionary = new
Dictionary<EntityKey, Entity>();

// build the dictionary....

List<Entitylist = new List<Entity>(dictionary.Values);


If it doesn't absolutely have to be a List<Entity>, you can use:

ValueCollection<Entitylist = dictionary.Values;

or:

ICollection<Entitylist = dictionary.Values;

or:

IEnumerable<Entitylist = dictionary.Values;
ValueCollection<Timplements both ICollection<Tand IEnumerable<T>.
If you want a list, the constructor of the list that takes the collection
will just copy the elements of the collection into a new list. If Entity
is a class, this means that it just copies the references.

--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #6

Sounds like a nightmare to maintain if the method can return a
dictionary in on situation or a list in another. Why not create two
methods wich different names then it's much clearer?

The List based on can call the Dictionary one and ither return
ICollection from the values or create a List depending on your needs.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 8 Mar 2007 08:43:11 -0800, "Andrew Robinson"
<ne****@nospam.nospamwrote:
>I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.

1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a dictionary or list?
thanks,

-Andy
Mar 8 '07 #7
Just an fyi to everyone: In the end, I created a method that takes a
delegate. Now you can create an anonymous method, pass it when you invoke
the method and create whatever collection type you want. Elegant solution!

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:q4********************************@4ax.com...
>
Sounds like a nightmare to maintain if the method can return a
dictionary in on situation or a list in another. Why not create two
methods wich different names then it's much clearer?

The List based on can call the Dictionary one and ither return
ICollection from the values or create a List depending on your needs.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 8 Mar 2007 08:43:11 -0800, "Andrew Robinson"
<ne****@nospam.nospamwrote:
>>I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.

1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain
either/both of these collection types and then how do you convert or cast
from the base to either a dictionary or list?
thanks,

-Andy
Mar 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
5
by: Stuart | last post by:
Hi everyone, I have this app that needs to insert about 400,000+ items into a sorted list, and I am currently using a simple insert something like this: int index = list.BinarySearch(t); if...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
4
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
by: Marc Gravell | last post by:
Well, I'd have to wonder why you don't just use Dictionary<,/ SortedList<,/ SortedDictionary<,(and just use .Keys and .Values) But something like below: List<KeyValuePair<DateTime, double>list...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a list of type object. The object has an ID as one of the elements and I would like to create another list that just has objects with unique IDs. For example in the list if I have...
15
by: hsachdevah | last post by:
Hello, I am trying to create a dictionary item with its key as list type and value as custom object type. It gives me a error "An item with the same key has already been added." Here is my code:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.