如何将Dictionary转换为到字典<字符串,字符串>在 c# 中

发布于 2024-10-16 07:13:00 字数 224 浏览 7 评论 0 原文

我有以下 C# 代码,

Dictionary<string, object> dObject = new Dictionary<string, object>();

我想将 dObject 转换为 Dictionary。我该怎么做?

I have below code in C#

Dictionary<string, object> dObject = new Dictionary<string, object>();

I want to convert dObject to Dictionary<string, string>. How can I do this?

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

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

发布评论

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

评论(6

情未る 2024-10-23 07:13:00

使用 ToDictionary 方法:

Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value.ToString());

在这里,您可以重用原始字典中的键,并使用 ToString 方法将值转换为字符串。

如果您的字典可以包含 null 值,则应在执行 ToString 之前添加 null 检查:

Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value == null ? "" : k.Value.ToString());

这样做的原因是 Dictionary 实际上是一个 IEnumerable>。上面的代码示例迭代可枚举对象并使用 ToDictionary 方法构建一个新字典。

编辑:

在 .Net 2.0 中,您不能使用 ToDictionary 方法,但您可以使用良好的老式 foreach 来实现相同的效果:

Dictionary<string, string> sd = new Dictionary<string, string>(); 
foreach (KeyValuePair<string, object> keyValuePair in dict)
{
  sd.Add(keyValuePair.Key, keyValuePair.Value.ToString());
}

编辑2:

如果您使用的是 .Net 2.0并且字典中可以有空值,以下应该是安全的:

Dictionary<string, string> sd = new Dictionary<string, string>(); 
foreach (KeyValuePair<string, object> keyValuePair in dict)
{
  sd.Add(keyValuePair.Key, keyValuePair.Value == null ? "" : keyValuePair.Value.ToString());
}

Use the ToDictionary method:

Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value.ToString());

Here you reuse the key from the original dictionary and you convert the values to strings using the ToString method.

If your dictionary can contain null values you should add a null check before performing the ToString:

Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value == null ? "" : k.Value.ToString());

The reason this works is that the Dictionary<string, object> is actually an IEnumerable<KeyValuePair<string,object>>. The above code example iterates through the enumerable and builds a new dictionary using the ToDictionary method.

Edit:

In .Net 2.0 you cannot use the ToDictionary method, but you can achieve the same using a good old-fashioned foreach:

Dictionary<string, string> sd = new Dictionary<string, string>(); 
foreach (KeyValuePair<string, object> keyValuePair in dict)
{
  sd.Add(keyValuePair.Key, keyValuePair.Value.ToString());
}

Edit2:

If you are on .Net 2.0 and you can have null values in the dictionary the following should be safe:

Dictionary<string, string> sd = new Dictionary<string, string>(); 
foreach (KeyValuePair<string, object> keyValuePair in dict)
{
  sd.Add(keyValuePair.Key, keyValuePair.Value == null ? "" : keyValuePair.Value.ToString());
}
梦初启 2024-10-23 07:13:00

.NET 3.5:

Dictionary<string, string> result = dObject.ToDictionary(kvp => kvp.Key, kvp => Convert.ToString(kvp.Value));

.NET 2.0:

Dictionary<string, string> result = new Dictionary<string, string>();
foreach (KeyValuePair<string, object> kvp in dObject) result.Add(kvp.Key, Convert.ToString(kvp.Value));

.NET 3.5:

Dictionary<string, string> result = dObject.ToDictionary(kvp => kvp.Key, kvp => Convert.ToString(kvp.Value));

.NET 2.0:

Dictionary<string, string> result = new Dictionary<string, string>();
foreach (KeyValuePair<string, object> kvp in dObject) result.Add(kvp.Key, Convert.ToString(kvp.Value));
影子是时光的心 2024-10-23 07:13:00

由于您使用的是.net 2.0:

Dictionary<string, string> dString = new Dictionary<string, string>();

foreach (KeyValuePair<string, object> item in dObject)
{
    dString.Add(item.Key, item.Value.ToString());
    //here item.Value.ToString() is an example
    //you should convert your item to string according to the requirement
}

Since you are using .net 2.0:

Dictionary<string, string> dString = new Dictionary<string, string>();

foreach (KeyValuePair<string, object> item in dObject)
{
    dString.Add(item.Key, item.Value.ToString());
    //here item.Value.ToString() is an example
    //you should convert your item to string according to the requirement
}
删除→记忆 2024-10-23 07:13:00

怎么样:

Dictionary<string, string> newDictionary = dObject.ToDictionary(k => k.Key, k => (string) k.Value);

How about:

Dictionary<string, string> newDictionary = dObject.ToDictionary(k => k.Key, k => (string) k.Value);
酒浓于脸红 2024-10-23 07:13:00

在 .Net 2.0 中,您最好的朋友可能是 foreach 循环:

Dictionary<string, object> dObject = new Dictionary<string, object>();
Dictionary<string, string> newObjects = new Dictionary<string, string>();

foreach (KeyValuePair<string, object> keyValuePair in dObject)
{
    newObjects.Add(keyValuePair.Key, keyValuePair.Value.ToString());
}

In .Net 2.0 probably your best friend is foreach loop:

Dictionary<string, object> dObject = new Dictionary<string, object>();
Dictionary<string, string> newObjects = new Dictionary<string, string>();

foreach (KeyValuePair<string, object> keyValuePair in dObject)
{
    newObjects.Add(keyValuePair.Key, keyValuePair.Value.ToString());
}
醉南桥 2024-10-23 07:13:00
Dictionary<string, string> dString = dObject.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString());

这只是想法。您可以插入任何验证。

Dictionary<string, string> dString = dObject.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString());

It's just idea. You can insert any verifications.

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