字典的多键查找

发布于 2024-09-28 19:55:29 字数 534 浏览 2 评论 0原文

我正在解析一个文件,我想将其存储在查找结构中,以便我可以使用两个键进行查找。

我有一个用户实体,其中包含名称、电子邮件id,类型无关。

我想将其存储在 Dictionary 中,这样我就可以通过查找 User 来获取用户 id。

我也想要相反的方式,即: Dictionary

我可以创建两个结构并进行查找。那很容易。我想用一个单一的结构来做到这一点。

我很好奇是否可以使用单个结构来完成此操作

,我想我可以这样做:

Dictionary ,然后实现 IEqualityComparer

是否有有更好的方法吗?

实施 IEqualityComparer 的最佳实践是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对你的占有欲 2024-10-05 19:55:29

无论您是否需要进行这种类型的映射,您都可以使用单个字典来完成您所要求的操作。这是一个粗略的示例:

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

当然,您可以组成任何您想要的字符串。如果您想将函数包装在事物周围,则可以避免每次获取项目时都必须强制转换对象。 (静态方法可能更适合您。)

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

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];
}
故事↓在人 2024-10-05 19:55:29

我不确定您的问题描述是否有意义,原因如下:

给定一个 User 对象,您知道 ID,因为 ID 是 User 的属性。因此,为什么您需要Dictionary?如果您有 Dictionary 您可以根据给定的 Id 找到用户,如果您有该用户,您应该已经拥有该 ID,从而不需要其他字典。

或者有时您有一个不完整的 User 对象,且 Id 未填充?

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?

悲念泪 2024-10-05 19:55:29

由于您将 id 存储在 User 对象中,因此不需要 User->id 映射。

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

谷夏 2024-10-05 19:55:29

您可以轻松创建两个字典,因为无论如何条目都是通过引用存储的。但正如其他人所说,如果您拥有包含该 ID 的用户,那么您似乎永远不需要找到该 ID。

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.

寄居人 2024-10-05 19:55:29

我知道这个问题已经很老了,但它出现在我刚刚进行的谷歌搜索中,而且我对最佳答案并不满意。我建议使用 Tuple 作为字典的键。如果 name 和 email 是字符串,id 是 int,那么它可能是一个 Tuple

I know this question is old, but it came up on a Google search I just did and I wasn't happy with the top answer. I would suggest using Tuple as your key into the dictionary. If name and email are strings, and id is an int, then it could be a Tuple

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文