有没有一种方法可以使用键搜索以多种方式搜索字典?

发布于 2024-12-10 03:42:53 字数 220 浏览 0 评论 0原文

基本上我有一个 Dictionary电影使用Guid收集和搜索电影,基本上就是movie.Guid。它工作得很好,但我也希望能够使用 movie.Name 搜索同一字典,而无需循环遍历每个元素。

这是可能的还是我必须为此创建另一个 Dictionary

Basically I have a Dictionary<Guid, Movie> Movies collection and search for movies using Guid, which is basically movie.Guid. It works great, but I also want to be able to search the same dictionary using movie.Name without looping through each element.

Is this possible or do I have to create another Dictionary<K, V> for this?

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

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

发布评论

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

评论(8

醉生梦死 2024-12-17 03:42:53

只需有两个字典,其中一个以 guid 作为其键,另一个以名称作为其键。

Just have two Dictionaries, one of them having the guid as its key and the other with the name as its key.

寒冷纷飞旳雪 2024-12-17 03:42:53

如果您不想查看每个元素,则需要向另一个方向对其进行索引。这意味着另一个字典的时间复杂度为O(1)

If you don't want to look at every element, you need to index it the other direction. This means another Dictionary to get O(1).

森林很绿却致人迷途 2024-12-17 03:42:53

您可以迭代变量,但随后您无法在字典中获取恒定时间搜索值(因为键的散列方式。)上面关于使用两个字典来散列对对象的引用的答案可能是一个很好的解决方案如果您没有太多要引用的对象。

You can iterate across the variables but then you arnt getting the constant-time searching value in a dictionary (because of the way that the keys are hashed.) The answer above regarding using two dictionarys to hash references to your object may be a good solution if you dont have too many objects to reference.

寻梦旅人 2024-12-17 03:42:53

您可以使用 Values 属性进行搜索:

dictionary.Values.Where(movie => movie.Name == "Some Name")

您将失去基于键的查找的效率,但它仍然有效。

You could search with the Values property:

dictionary.Values.Where(movie => movie.Name == "Some Name")

You'll lose the efficiency of a key based look up, but it will still work.

空城缀染半城烟沙 2024-12-17 03:42:53

由于字典用于单向映射,因此您无法从值获取键。

你需要两本词典。

还有一个建议:
您可以对键使用自定义哈希函数而不是 GUID,并将“电影名称”哈希存储为键。然后您实际上可以在词典中执行双向搜索。

Since dictionaries are for one-way mapping you can't get keys from values.

You'll need two dictionaries.

There is also a suggestion:
You can use a custom hash function for keys instead of GUIDs and store Movie Names hash as keys. Then you can actually perform two way search in your dictionary.

千笙结 2024-12-17 03:42:53

与其使用两个字典,不如使用一个内部有两个字典的容器类。

一些名叫乔恩的人想出了一个部分解决方案(您可以轻松地在此基础上构建),将他的代码留在这里:获取通用字典的键值?

Rather than using two dictionaries, you'd be much better off using one container class that has two dictionaries inside it.

Some guy named Jon came up with a partial solution to this (which you could easily build upon), leaving his code here: Getting key of value of a generic Dictionary?

舞袖。长 2024-12-17 03:42:53

您无法使用该字典以相同的效率进行搜索。但是您可以轻松地针对字典的 Values 属性运行 LINQ 查询,该属性只是 Movie 值的集合。

var moviesIWant = From m in movieLookup.Values
                  Where m.Name == "Star Wars"
                  Select m

一些想法:

  • 当您找到答案时,您将不会拥有指南,除非它们也是电影的财产。
  • 对于一本小词典来说,这已经足够了。对于大型且重复的搜索,您应该考虑根据您希望搜索的其他值创建其他字典。只有这样,你才能达到与原始字典相当的 guid 查找速度。

您可以创建另一个按名称键控的字典。完成此操作后,您可以通过关键字搜索该词典,即使对于非常大的词典,它也将具有与原始词典相同的超级效率。

var moviesByName = movieLookup.Values.ToDictionary(m => m.Name, m => m)

You can't use that dictionary to do that search with anything like the same efficiency. But you can easily just run a LINQ query against your dictionary's Values property, which is just collection of the Movie values.

var moviesIWant = From m in movieLookup.Values
                  Where m.Name == "Star Wars"
                  Select m

Some thoughts:

  • When you find your answer though, you would not have the guids, unless they were also a property of movie.
  • For a small dictionary, this is just fine. For large and repeated searches, you should consider the creation of other dictionaries keyed on the other values you wish to search on. Only in this way would you achieve the speed of a guid lookup comparable to your original dictionary.

You could create another dictionary keyed by Name. Once you've done this, you could search this dictionary by it's key and it would have the same super-efficiency of your original dictionary, even for a very large dictionary.

var moviesByName = movieLookup.Values.ToDictionary(m => m.Name, m => m)
压抑⊿情绪 2024-12-17 03:42:53

不,我不相信这是可能的。你必须使用另一本字典。

如果您想要搜索更多电影属性,您最好将数据移至数据库并使用它进行查询。毕竟这就是数据库的好处。

No I don't believe it is possible. You'll have to use another dictionary.

If you are going to want to search on more movie attributes you may be better off moving the data down to a database and use that for querying. That is what databases are good for after all.

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