可能重复:
C# Sortable collection which allows duplicate keys
基本上,我想让Dictionary使用重复的键,而不是进入自定义的比较器实现。有一种想法是:
Dictionary<key, List<value>>
但它仍然有一些开销。我希望字典有"AllowDuplicates“。
发布于 2009-02-16 00:24:05
如果你使用的是.NET 3.5,那么Lookup可能就是你想要的。
发布于 2009-02-16 00:23:51
.NET 2.0:PowerCollections包含OrderedMultiDictionary
。
发布于 2011-10-14 23:00:16
您仍然可以使用SortedList,并尝试通过将值和Guid组合到一个类中来创建一个惟一的键。在这种情况下,您必须为新密钥实现IComparer<NewKey>
,如下所示:
class MyKey
{
public Guid Guid { get; set; }
public float Value { get; set; }
}
class MyComparer : IComparer<MyKey>
{
public int Compare(MyKey x, MyKey y)
{
if (x == null || y == null)
throw new InvalidOperationException("both of parameters must be not null");
if (x.Value < y.Value) return -1;
if (x.Value > y.Value) return 1;
return 0;
}
}
然后
var mySortedList = new SortedList<MyKey, MyValue>(new MyComparer());
https://stackoverflow.com/questions/551901
复制相似问题