C# 遍历 NameValueCollection

发布于 2024-10-18 04:15:09 字数 467 浏览 3 评论 0原文

我有一个 NameValueCollection,并且想要迭代这些值。目前,我正在这样做,但似乎应该有一种更简洁的方法来做到这一点:

NameValueCollection nvc = new NameValueCollection();
nvc.Add("Test", "Val1");
nvc.Add("Test2", "Val1");
nvc.Add("Test2", "Val1");
nvc.Add("Test2", "Val2");
nvc.Add("Test3", "Val1");
nvc.Add("Test4", "Val4");

foreach (string s in nvc)
    foreach (string v in nvc.GetValues(s))
        Console.WriteLine("{0} {1}", s, v);

Console.ReadLine();

有吗?

I have a NameValueCollection, and want to iterate through the values. Currently, I’m doing this, but it seems like there should be a neater way to do it:

NameValueCollection nvc = new NameValueCollection();
nvc.Add("Test", "Val1");
nvc.Add("Test2", "Val1");
nvc.Add("Test2", "Val1");
nvc.Add("Test2", "Val2");
nvc.Add("Test3", "Val1");
nvc.Add("Test4", "Val4");

foreach (string s in nvc)
    foreach (string v in nvc.GetValues(s))
        Console.WriteLine("{0} {1}", s, v);

Console.ReadLine();

Is there?

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

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

发布评论

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

评论(8

一袭水袖舞倾城 2024-10-25 04:15:09

您可以使用 Linq 展平集合,但它仍然是一个 foreach 循环,但现在更加隐式。

var items = nvc.AllKeys.SelectMany(nvc.GetValues, (k, v) => new {key = k, value = v});
foreach (var item in items)
    Console.WriteLine("{0} {1}", item.key, item.value);

第一行将嵌套集合转换为具有属性 keyvalue 的匿名对象的(非嵌套)集合。

它被扁平化了,现在它是一个映射键 ->值而不是键 ->值的集合。示例数据:

之前:

测试-> [瓦尔],

测试2-> [Val1,Val1,Val2],

测试3-> [Val1],

测试4-> [Val4]

之后:

测试->瓦尔,

测试2->值1,

测试2->值1,

测试2->值2,

测试3->值1,

测试4->瓦尔4

You can flatten the collection with Linq, but it's still a foreach loop but now more implicit.

var items = nvc.AllKeys.SelectMany(nvc.GetValues, (k, v) => new {key = k, value = v});
foreach (var item in items)
    Console.WriteLine("{0} {1}", item.key, item.value);

The first line, converts the nested collection to a (non-nested) collection of anonymous objects with the properties key and value.

It's flatten in the way that it's now a mapping key -> value instead of key -> collection of values. The example data:

Before:

Test -> [Val],

Test2 -> [Val1, Val1, Val2],

Test3 -> [Val1],

Test4 -> [Val4]

After:

Test -> Val,

Test2 -> Val1,

Test2 -> Val1,

Test2 -> Val2,

Test3 -> Val1,

Test4 -> Val4

风铃鹿 2024-10-25 04:15:09

您可以使用键进行查找,而不是使用两个循环:

foreach (string key in nvc)
{
    Console.WriteLine("{0} {1}", key, nvc[key]);
}

You can use the key for lookup instead of having two loops:

foreach (string key in nvc)
{
    Console.WriteLine("{0} {1}", key, nvc[key]);
}
可爱暴击 2024-10-25 04:15:09

这里没有什么新东西可看(@Julian's +1'd by me 的答案在功能上是等效的),请大家继续前进。


我有一组[对于这种情况来说太过分但可能相关]扩展方法在相关问题的答案中,这将让你做:

foreach ( KeyValuePair<string,string> item in nvc.AsEnumerable().AsKeyValuePairs() )
    Console.WriteLine("{0} {1}", item.key, item.value);

Nothing new to see here (@Julian's +1'd by me answer is functionally equivalent), y'all move along y'all please.


I have an [overkill for this case but possibly relevant] set of extension methods in an answer to a related question, which would let you do:

foreach ( KeyValuePair<string,string> item in nvc.AsEnumerable().AsKeyValuePairs() )
    Console.WriteLine("{0} {1}", item.key, item.value);
怎樣才叫好 2024-10-25 04:15:09
var enu = myNameValueCollection.GetEnumerator();
while (enu.MoveNext())
{
    string key = (string)enu.Current;
    string value = myNameValueCollection[key];
}

或者当键可为空时:

for (int i = 0; i < myNameValueCollection.Count; i++)
{
    string key = myNameValueCollection.GetKey(i);
    string value = myNameValueCollection.Get(i);
}
var enu = myNameValueCollection.GetEnumerator();
while (enu.MoveNext())
{
    string key = (string)enu.Current;
    string value = myNameValueCollection[key];
}

OR when keys nullable:

for (int i = 0; i < myNameValueCollection.Count; i++)
{
    string key = myNameValueCollection.GetKey(i);
    string value = myNameValueCollection.Get(i);
}
故事与诗 2024-10-25 04:15:09

我认为这更简单:VB.NET

For i As Integer = 0 To nvc.Count - 1
   Console.Write("No", "Key", "Value")
   Console.Write(i, nvc.GetKey(i), nvc.Get(i))
Next

C#:

for (int i = 0; i <= nvc.Count - 1; i++)
{
    Console.Write("No", "Key", "Value");
    Console.Write(i, nvc.GetKey(i), nvc.Get(i));
}

I think this is simpler: VB.NET

For i As Integer = 0 To nvc.Count - 1
   Console.Write("No", "Key", "Value")
   Console.Write(i, nvc.GetKey(i), nvc.Get(i))
Next

C#:

for (int i = 0; i <= nvc.Count - 1; i++)
{
    Console.Write("No", "Key", "Value");
    Console.Write(i, nvc.GetKey(i), nvc.Get(i));
}
美煞众生 2024-10-25 04:15:09

我发现避免嵌套循环的唯一方法是使用附加列表来存储值:(

List<string> arrValues = new List<string>();
for (int i = 0; i < nvc.Count; i++)
    arrValues.AddRange(nvc.GetValues(i));
foreach (string value in arrValues)
    Console.WriteLine(value);

需要[仅] .NET 2.0 或更高版本)

The only way I found to avoid the nested loops is using additional List to store the values:

List<string> arrValues = new List<string>();
for (int i = 0; i < nvc.Count; i++)
    arrValues.AddRange(nvc.GetValues(i));
foreach (string value in arrValues)
    Console.WriteLine(value);

(Requires [only] .NET 2.0 or later)

清风无影 2024-10-25 04:15:09
for (int i = 0; i < nvc.Count; i++) {
    System.Console.WriteLine(

        nvc.AllKeys[i] + " = " + nvc[i]

    );
}
for (int i = 0; i < nvc.Count; i++) {
    System.Console.WriteLine(

        nvc.AllKeys[i] + " = " + nvc[i]

    );
}
软糖 2024-10-25 04:15:09
foreach ( string key in nvc.Keys )
   Console.WriteLine("{0} {1}", key, nvc[key]);

这将返回所有键和相应的值。

foreach ( string key in nvc.Keys )
   Console.WriteLine("{0} {1}", key, nvc[key]);

This will return you all keys and corresponding values.

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