views:

52

answers:

4

I am parsing a file and I would like to store it in a lookup structure in a way that I can lookup with two keys.

I have a User Entity that has name, email and id, types are irrelevant.

I would like to store it in a Dictionary<User, id> so I can get the user id by looking up with User.

I also want the other way around, ie : Dictionary<Id, User>

I can create two structures and do the lookup. That s easy. I d like to do it with a single structure.

I am curious if I can do this with a single structure

I was thinking that I can do:

Dictionary<User, User> , then implement a IEqualityComparer<User>

Is there a better way to do this?

What would be the best practice to implement IEqualityComparer?

A: 

Since you store the id in the User object, you don't need a User->id mapping.

Marcelo Cantos
Yes, I can store it. and it s in there for that matter.
+1  A: 

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

Given a User object you know the ID, as the ID is a property of the User. Therefore, why would you ever need the Dictionary<User, Id>? If you have the Dictionary<Id, User> you can get to the user given the Id, and if you have the user you should already have the ID, making the other dictionary unnecessary.

Or is it the case that sometimes you have an incomplete User object with the Id not populated?

mtreit
At some point, I know the User {name and email} but not the Id, and at some point I know the User id but not User {name and email}. makes sense?
In that case it sounds like you need two dictionaries: one that does lookup on email (concatenate the name as well if email is not unique) and one that does lookup on Id. You don't need (or pobably want the overhead of) to use the entire User object as a key. I would be inclined to just use two dictionaries: it's simple and clean. You could get away with storing the same object twice in a dictionary using a key type that can represent either the email or the id, but I don't see what that buys you.
mtreit
A: 

You could easily create two dictionaries since the entries are stored by reference anyway. But as other have said it seams like you would never need to find the ID if you have the user which contains the id.

rerun
yeah that s easy. I think i cant get it with a Custom comparator..
+1  A: 

Whether you need to do this type of mapping or not, you can use a single dictionary to something like what you're asking for. Here's a rough sample:

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 up any string you want. And if you want to wrap functions around things, you can avoid having to cast the object each time you get an item. (A static method might work better for you.)

User GetUser(int id, Dictionary<string, object> dict) 
{
    return (User)dict["ID_" + id];
}
John Fisher
that s clever and should work. How about without hardcoding any string?
@user177883: Then, you would end up with basically a **Dictionary<object, object>** It probably wouldn't be worth it.
John Fisher