C# 是否有没有唯一键的 Dictionary<Key,Value>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11409587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:45:40  来源:igfitidea点击:

Is there Dictionary<Key,Value> without unique key?

c#.net

提问by eugeneK

Possible Duplicate:
Duplicate keys in .NET dictionaries?

可能的重复:
.NET 字典中的重复键?

I need to create a Collection<string,object>, one is a string and another one is an object. I cannot use Dictionary since none of items is unique.

我需要创建一个Collection<string,object>,一个是字符串,另一个是对象。我不能使用字典,因为没有一个项目是唯一的。

Is there any way to create List<T,T>? Since i don't want to create an object to hold both values just to use it in single foreach loop.

有没有办法创建List<T,T>?因为我不想创建一个对象来保存两个值只是为了在单个 foreach 循环中使用它。

Thanks

谢谢

采纳答案by Adam Houldsworth

Try List<Tuple<string, object>>. Then you can get the usual linq support over the list to get at each Tuple<string, object>.

试试List<Tuple<string, object>>。然后,您可以通过列表获得通常的 linq 支持以获取每个Tuple<string, object>.

The Tuple<,>classis available as of .NET 4 onwards, but you can easily mimick a tuple type of your own without too much hassle.

Tuple<,>可作为.NET 4中开始的,但你可以很容易地模拟天生一个属于自己的元组类型没有太多的麻烦。

I think that tuples are considered equal based on their item values, but with objectthis would likely be reference equals and thus shouldn't likely be equal... if that made any sense!

我认为元组根据它们的项目值被认为是相等的,但这object可能是引用相等,因此不应该是相等的......如果这有意义的话!

More info on equality here:

关于平等的更多信息:

Surprising Tuple (in)equality

令人惊讶的元组(不)相等

Update:

更新:

If you need to associate a list of items with a key for the purposes of a lookup, the duplicate question answer correctly highlights the Lookupclass (in your case, Lookup<string, object>), which would express your intent a little clearer than a List<Tuple<string, object>>.

如果您需要将项目列表与键相关联以进行查找,则重复的问题答案会正确突出显示Lookup该类(在您的情况下,Lookup<string, object>),这会比 a 更清楚地表达您的意图List<Tuple<string, object>>

回答by Govind Malviya

Try to create your custom class and use it

尝试创建您的自定义类并使用它

    class KeyValue<Tkey,TValue>
    {
       public Tkey Key {get;set;}
       public TValue Value {get;set;}
    }

use like

使用喜欢

List<KeyValue<string,string>> list = new List<KeyValue<string,string>>();

回答by Rzv.im

you can use

您可以使用

List<KeyValuePair<string,object>>

or

或者

Dictionary<string,List<object>>

回答by npinti

Maybe you could create a List of tuples using the Tupleclass? Something like so:

也许您可以使用Tuple类创建一个元组列表?像这样:

List<Tuple<String, Object>> list = new List<Tuple<String, Object>>();

回答by Tianzhen Lin

Have you considered using "Group by" then turned the grouped result into dictionary?

您是否考虑过使用“分组依据”然后将分组结果转换为字典?

回答by D-loader

If you're using .NET 4, you can use the Tuple ( http://msdn.microsoft.com/en-us/library/system.tuple.aspx) object:

如果您使用 .NET 4,则可以使用元组 ( http://msdn.microsoft.com/en-us/library/system.tuple.aspx) 对象:

var list = new List<Tuple<string, object>>();

if you're using an earlier version of .NET, you can implement the Tuple class yourself: Equivalent of Tuple (.NET 4) for .NET Framework 3.5

如果您使用的是 .NET 的早期版本,您可以自己实现 Tuple 类:等效于 .NET Framework 3.5 的 Tuple (.NET 4)