- Home /
get key of item from dictionary based on value c#
Hi, I'm trying to get from Dictionary one of values as string but I don't know how :(
public class Items
{
public string name;
public Vector3 position;
// and some more variables
// Instantiate
public Items(string _name, Vector3 _position)
{
this.name = _name;
this.position = _position;
}
}
// and later in the same script
public class allBubblesGrid : MonoBehaviour {
public Dictionary<string, Items> inventory = new Dictionary<string, Items>();
public void ModifyItem(Vector3 newPosition)
{
// here I want to get name (key) from dictionary based on its position
// As far i can get bool which says that item exist:
if(inventory.Any(tr => tr.Value.position.Equals(newPosition)))
Debug.Log("Exist");
// but I want something like this:
string nameCopy = /* get name of item based on position */ ### cannot get this ###
Items modifiedItem = new Items(nameCopy, newPosition);
inventory[nameCopy] = modifiedItem;
}
}
Answer by phxvyper · Mar 24, 2017 at 10:14 PM
You can use Linq to select all of the items where there is a certain value:
var keys = dict.Where(item => item.Value.position.Equals(newPosition))
.Select(item => item.Key);
Its important to note that this is not very efficient with a complexity of O(N) since it has to go through every single item to find all of the items that have the value you're looking for. Dictionary's are intended for fast lookup via a key rather than a value so this method should be used sparingly.
TL;DR if you're doing this not too often, then you should be fine. Since this is inventory related, you probably wont be calling this every frame, so this method should work.
Hi @phxvyper, I need it to check them by position only once, later I will call them by name so that's shouldn't be a problem. I don't know how to go further with this, after I put line you gave:
var keys = dict.Where(item => item.Value.position.Equals(newPosition))
.Select(item => item.$$anonymous$$ey);
i don't know how to make an item with that name:
Items modifiedItem = new Items(keys, newPosition); // line 215
inventory[nameCopy] = modifiedItem; // line 216
// I've got this in console:
// 1 // Assets/Scripts/Inventory.cs(215,28): error CS1502: The best overloaded method match for `Items.Items(string, UnityEngine.Vector3)' has some invalid arguments
// 2 // Assets/Scripts/Inventory.cs(215,42): error CS1503: Argument `#1' cannot convert `System.Collections.Generic.IEnumerable<string>' expression to type `string'
if I use debug I've got this:
Debug.Log(keys); // line 218
System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[System.Collections.Generic.$$anonymous$$eyValuePair`2[System.String,Items],System.String]
UnityEngine.Debug:Log(Object)
Inventory:$$anonymous$$odifyItem(String, Vector3) (at Assets/Scripts/Inventory.cs:218)
Inventory:Start() (at Assets/Scripts/Inventory.cs:112)
Your answer
Follow this Question
Related Questions
Dictionary wont give me value with correct key 1 Answer
why isn't my lock working 1 Answer
Problem with dictionary inside of a class 0 Answers
Cannot use custom type as key for dictionary 0 Answers
Comparing Dictionary values 1 Answer