我有以下疑问:
private Dictionary<string, Dictionary<string, File>> listFiles = new Dictionary<string,Dictionary<string,File>>();
如何将项目添加到字典中?有没有更好的方法来做这样的事情?
信息:存储源文件名、目标文件名和文件本身。
Edit1:刚刚弄清楚,我想要存储的是3个值,其中外部字典的第二个对象存储一个字典对象,这并不是最好的方法,因为它总是只包含一个KeyValuePair 。
Edit2:对于文件,我指的是二进制数据。
Edit3:我有一个未排序的文件列表,我需要对其进行排序,然后发送到其他地方。
I have the following decleration:
private Dictionary<string, Dictionary<string, File>> listFiles = new Dictionary<string,Dictionary<string,File>>();
How do i add an item to the dictionary? Is there a better way to do something like this?
Info: That stores a sourcefilename, destinationfilename, and the file itself.
Edit1: Just figured it out, all I want to store is 3 values, where the second object of the outer dictionary stores a dictionary object, which isn't really the best way to do it, seeing that it will always contain just one KeyValuePair.
Edit2: With File i meant the binary data.
Edit3: I have a unsorted file list, which i need to sort, and then send somewhere else.
发布评论
评论(5)
你可以使用
希望这有帮助!
you can use
Hope this helps!!!
您可以编写一个包装类
Dictionary :字典>
You can write a wrapper class
Dictionary<TKey1, TKey2, TValue> : Dictionary<TKey1, Dictionary<TKey2, TValue>>
要添加新文件,您需要执行以下操作:
请注意,这将覆盖现有源文件中的任何映射。然而,似乎您真正想要的是来自(来源)的地图 -> (dest, File),所以在这种情况下,我将创建一个类来包含目标文件名和文件,然后创建一个字典来包含此查找:
然后创建一个
Dictionary
To add a new File you'd do something like:
Note this will overwrite any mapping from an existing source file. However it seems what you really want is a map from (source) -> (dest, File), so in this case I'd make a class to contain the destination filename and the file and then create a dictionary to contain this lookup:
and then create a
Dictionary<string, DestinationFileInfo>
我在名为 DoubleKeyDictionary 的课程中这样做过一次。我需要字典对象的功能,但我有 2 个键。如果您将数据锁定得非常紧,您可以尝试仅附加它,例如从键 #1 中获取字符串,添加 ~ 或一些分隔符,然后添加字符串 #2,例如 blue~large,这将为您提供唯一的键。同样,这对于字符串效果更好,并且需要您锁定数据,这样字符串中的 ~ 就不会影响整个事情。
所以我最初所做的就是你所做的:Dictionary>,但语法有点笨拙。因此,尽管它没有明确回答您的问题,但这是我的解决方案:
创建一个名为 MultiKeyDictionary 的类,它内部包含一个 DataTable,并且有一个用于检索 Customer 对象或其他内容的方法。该方法采用 params object[],并使用 DataTable.Select 方法获取该对象。这样你就可以拥有任意数量的钥匙。当然,您需要添加对象的方法。
I did this once in a class I called DoubleKeyDictionary. I needed the functionality of a dictionary object, but I had 2 keys. If you have the data locked down really tight, you might try just appending it, e.g. take the string from key #1, add a ~ or some delimiter, and then add string #2, like blue~large, which would give you unique keys. Again, this works better with strings, and requires you lock the data down so a ~ in your string doesn't fubar the whole thing.
So what I did was initially what you did: Dictionary>, but the syntax was a little clumsy. So although it doesn't answer your question explicitly, here's my solution:
Create a class called MultiKeyDictionary, it contains a DataTable internally, and has a method for retrieving your Customer object or whatever. That method takes a params object[], and uses the DataTable.Select method to get the object. That way you can have as many keys as you like. You'll need methods for adding objects of course.
当您发现自己将一个泛型嵌套在另一个泛型中时,请考虑创建一个类。例如:
来代替:
现在,您可以使用以下内容
When you find yourself nesting one generic inside another, consider creating a class. For example:
Now, instead of this:
you can work with this: