Search for multiple keywords for a dictionary

I am parsing a file, and I would like to save it in the search structure so that I can search for two keys.

I have User Entityone that has name, emailand idtypes don't matter.

I would like to save it in Dictionary<User, id>so that I can get the user id by viewing it with the user.

I also need another way: Dictionary<Id, User>

I can create two structures and do a search. It is easy. I would like to do this with one structure.

I'm curious if I can do this with a single structure

I thought I could do:

Dictionary<User, User>then do IEqualityComparer<User>

Is there a better way to do this?

What would be the best practice for implementing IEqualityComparer?

+3
source share
5 answers

If you need to do this type of mapping or not, you can use one dictionary for something like what you are asking for. Here is an example:

var dict = new Dictionary<string, object>();
dict["ID_001"] = new User();
dict["USER_??"] = 001; // Need a unique user string to replace the "??"

Of course, you can make any line you want. And if you want to wrap functions around things, you can avoid having to throw an object every time you get an element. (A static method might work better for you.)

User GetUser(int id, Dictionary<string, object> dict) 
{
    return (User)dict["ID_" + id];
}
+1
source

I'm not sure your description of the problem makes sense for the following reason:

User, , . Dictionary<User, Id>? Dictionary<Id, User>, , , ID, .

, User Id, ?

+1

User, User- > id.

0

, . , , , , , .

0

I know this question is old, but it appeared in a Google search that I just did, and I was not happy with the best answer. I would suggest using Tuple as a key in a dictionary. If the name and email address are strings, and id is int, then this may be Tuple

0
source

Source: https://habr.com/ru/post/1770798/


All Articles