如何在通用字典中使用通用字典?

发布于 2024-08-24 09:05:15 字数 406 浏览 5 评论 0 原文

我有以下疑问:

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.

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

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

发布评论

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

评论(5

心凉怎暖 2024-08-31 09:05:15

你可以使用

Dictionary<string, KeyValuePair<string, File>>

希望这有帮助!

you can use

Dictionary<string, KeyValuePair<string, File>>

Hope this helps!!!

诗酒趁年少 2024-08-31 09:05:15

您可以编写一个包装类 Dictionary :字典>

You can write a wrapper class Dictionary<TKey1, TKey2, TValue> : Dictionary<TKey1, Dictionary<TKey2, TValue>>

月牙弯弯 2024-08-31 09:05:15

要添加新文件,您需要执行以下操作:

listFiles[srcFile] = new Dictionary<string, File>();
listFiles[srcFile][destFile] = file;

请注意,这将覆盖现有源文件中的任何映射。然而,似乎您真正想要的是来自(来源)的地图 -> (dest, File),所以在这种情况下,我将创建一个类来包含目标文件名和文件,然后创建一个字典来包含此查找:

public class DestinationFileInfo { ... }

然后创建一个 Dictionary

To add a new File you'd do something like:

listFiles[srcFile] = new Dictionary<string, File>();
listFiles[srcFile][destFile] = file;

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:

public class DestinationFileInfo { ... }

and then create a Dictionary<string, DestinationFileInfo>

扛起拖把扫天下 2024-08-31 09:05:15

我在名为 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.

甜扑 2024-08-31 09:05:15

当您发现自己将一个泛型嵌套在另一个泛型中时,请考虑创建一个类。例如:

// todo: give this a name that better describes its purpose
public class FileContainer
{
    string DestinationFileName { get; set; }
    File File { get; set; }
}

来代替:

Dictionary<string, KeyValuePair<string, File>>

现在,您可以使用以下内容

Dictionary<string, FileContainer>

When you find yourself nesting one generic inside another, consider creating a class. For example:

// todo: give this a name that better describes its purpose
public class FileContainer
{
    string DestinationFileName { get; set; }
    File File { get; set; }
}

Now, instead of this:

Dictionary<string, KeyValuePair<string, File>>

you can work with this:

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