复合关键词典 [英] Composite Key Dictionary

查看:100
本文介绍了复合关键词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在List中有一些对象,我们假设 List&MyClass> 和MyClass有几个属性。我想根据MyClass的3个属性创建列表的索引。在这种情况下,2个属性是int,一个属性是datetime。



基本上我希望能够执行以下操作:

 词典< CompositeKey,MyClass> MyClassListIndex = Dictionary< CompositeKey,MyClass>(); 
//将列表< MyClass>中的项目填入字典MyClassList
MyClass aMyClass = Dicitonary [(keyTripletHere)];

我有时会在列表中创建多个字典来索引其所持有的类的不同属性。我不知道如何最好地处理复合键。我考虑做三个值的校验和,但是这样会产生碰撞的风险。

解决方案

你应该使用元组。它们相当于一个CompositeKey类,但是Equals()和GetHashCode()已经为您实现。

  var myClassIndex =新字典< Tuple< int,bool,string>,MyClass>(); 
//使用列表< MyClass>中的项目填充字典MyClassList
foreach(myClassList中的var myObj)
myClassIndex.Add(Tuple.Create(myObj.MyInt,myObj.MyBool,myObj.MyString),myObj);
MyClass myObj = myClassIndex [Tuple.Create(4,true,t)];

或使用System.Linq

  var myClassIndex = myClassList.ToDictionary(myObj => Tuple.Create(myObj.MyInt,myObj.MyBool,myObj.MyString)); 
MyClass myObj = myClassIndex [Tuple.Create(4,true,t)];

除非您需要自定义哈希的计算,否则使用元组更简单。



如果要在复合键中包含很多属性,则元组类型名称可能会变得很长,但您可以通过创建自己的类来缩小名称元组






** 于2017年修改 **



有一个从C#7开始的新选项:值元组。这个想法是一样的,但是语法是不一样的:



类型 Tuple< int,bool,string> 成为(int,bool,string),值 Tuple.Create(4,true,t)成为(4,true,t)



使用值元组,命名元素。请注意,演出略有不同,所以如果对你很重要,可能需要做一些基准测试。


I have some objects in List, let's say List<MyClass> and MyClass has several properties. I would like to create an index of the list based on 3 properties of of MyClass. In this case 2 of the properties are int's, and one property is a datetime.

Basically I would like to be able to do something like:

Dictionary< CompositeKey , MyClass > MyClassListIndex = Dictionary< CompositeKey , MyClass >();
//Populate dictionary with items from the List<MyClass> MyClassList
MyClass aMyClass = Dicitonary[(keyTripletHere)];

I sometimes create multiple dictionaries on a list to index different properties of the classes it holds. I am not sure how best to handle composite keys though. I considered doing a checksum of the three values but this runs the risk of collisions.

解决方案

You should use tuples. They are equivalent to a CompositeKey class, but the Equals() and GetHashCode() are already implemented for you.

var myClassIndex = new Dictionary<Tuple<int, bool, string>, MyClass>();
//Populate dictionary with items from the List<MyClass> MyClassList
foreach (var myObj in myClassList)
    myClassIndex.Add(Tuple.Create(myObj.MyInt, myObj.MyBool, myObj.MyString), myObj);
MyClass myObj = myClassIndex[Tuple.Create(4, true, "t")];

Or using System.Linq

var myClassIndex = myClassList.ToDictionary(myObj => Tuple.Create(myObj.MyInt, myObj.MyBool, myObj.MyString));
MyClass myObj = myClassIndex[Tuple.Create(4, true, "t")];

Unless you need to customize the computation of the hash, it's simpler to use tuples.

If there are a lot of properties you want to include in the composite key, the Tuple type name can become pretty long, but you can make the name shorter by creating your own class deriving from Tuple<...>.


** edited in 2017 **

There is a new option starting with C# 7: the value tuples. The idea is the same, but the syntax is different, lighter:

The type Tuple<int, bool, string> becomes (int, bool, string), and the value Tuple.Create(4, true, "t") becomes (4, true, "t").

With value tuples, it also becomes possible to name the elements. Note that performances are slightly different, so you may want to do some benchmarking if they matter for you.

这篇关于复合关键词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆