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 7 57453
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
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
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?]
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:
...
|
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:
|
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...
|
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...
|
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...
|
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...
|
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:...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |