views:

218

answers:

5

Hello,

I have a "Dictionary" data base and I want to return an element from a specific location. I saw that there is "ElementAt" function but I didnt manage to use it.

Why doesnt something like that work?

closeHash.ElementAt<State>(i);

it tells me the following error:

Error 3 'System.Collections.Generic.Dictionary' does not contain a definition for 'ElementAt' and the best extension method overload 'System.Linq.Queryable.ElementAt(System.Linq.IQueryable, int)' has some invalid arguments

And this code also doesnt work because closeHash[i] gives me only the index and not the actual element:

   if (closeHash.ContainsKey(i) && ((State)closeHash[i]).getH() + 
((State)closeHash[i]).getG() > checkState.getH() + checkState.getG()

Each element in the Dictionary is of a class "State" and checkState also is a State that has GetH and GetG Functions. I want to take out the element at the Ith position and work on it and not just remove it.

thanks in advance!

Greg

+1  A: 

I'm sure you can do it somehow, but hash table type collections don't usually work on the concept of "order". In Java you could get an Enumerator or Iterator and remove the nth item you come across, but again, I don't think that's meaningful.

Jonathon
A: 

You just need to use System.Linq to be able to use the ElementAt<> extension method. Include this in the beginning of the class declaration:

using System.Linq;

And that should do the trick.

Gergely Orosz
A: 

How about using the Remove function and pass in the ElementAt?

        Dictionary<int, string> closeHash = new Dictionary<int, string>();
        closeHash.Add(47, "Hello");
        closeHash.Remove(closeHash.ElementAt(0).Key);
Lucas B
A: 

The error message impleas that your variable closeHash is a Dictionary, apparently a Dictionary<"type of i", State>. If not, please specify the exact declaration of the dictionary and "i".

Then closeHash[i] should give a value of type State, so you don't need to cast.

As others have said, a Dictionary has no concept of "order" and thus not of a "n-th item".

Hans Kesting
+2  A: 

Using the Dictionary in the Generic collections, you should never have to use a RemoveAt(). The Key values in a dictionary must be unique.

//       Unique Not Unique
//          |     |   
Dictionary<int, string> alphabet = new Dictionary<int, string>();
alphabet.Add(1, "A");
//Adding this will cause an Argument Exception to be thrown
//The message will be: An item with the same key has already been added.
alphabet.Add(1, "A");

If I wanted to remove an item with the key 24 from my alphabet example, this is what I would need:

alphabet.Remove(24)

This works because there will never be 2 keys with the same value.

Now, if you want to remove an item with out knowing it's key, that is a different story. You would need to go through every element and try to find the key associated with it. I would use linq, kind of like this:

var key = (from item in alphabet
             where item.Value == "K"
             select item.Key).FirstOrDefault();
//Checking to make sure key is not null here
...
//Now remove the key
alphabet.Remove(key)

Both ways, there is never a reason, that I can see, to need RemoveAt(index) from any list where the key values must be unique.

thorkia