Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kamil1064 · Mar 24, 2017 at 08:03 PM · classvaluekeydictionary

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;
 }
 }









Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kamil1064 · Mar 26, 2017 at 02:50 PM 0
Share

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)







avatar image Kamil1064 Kamil1064 · Mar 29, 2017 at 02:47 PM 0
Share

BU$$anonymous$$P... :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges