如何设置NameValueCollection的长度

发布于 2024-09-26 10:39:47 字数 122 浏览 0 评论 0 原文

我是 C# 新手(不是编程新手)。请问,减少 NameValueCollection 对象中项目数量的最简单方法是什么?例如,我的集合中有 13 个名称-值对,我想将其剪切到前 5 个。我真的必须循环执行吗?

谢谢

I am a newbie in C# (not in programming). Please, what is the simplest way to decrease items count in NameValueCollection object? For example I have 13 name-value pairs in collection and I want to cut it to the first 5. Do I really have to do it in a loop?

thanx

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

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

发布评论

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

评论(4

梦魇绽荼蘼 2024-10-03 10:39:47

如果您使用的是 C# 4.0 并且扩展方法适用于您的集合,则您应该能够简单地编写 var lFirst5 = lCollection.Take(5)

嗯。

刚刚查看了 NameValueCollection,我发现您无法对其执行 .Take() 操作。我个人会使用不同的集合...也许List>

If you're using C# 4.0 and the extension methods work on your collection, you should be able to simply write var lFirst5 = lCollection.Take(5).

Meh.

Just had a look at NameValueCollection and I see you can't do .Take() on it. I'd use a different collection personally... maybe List<KeyValuePair<string, object>>?

慵挽 2024-10-03 10:39:47

如果您确实想使用 NameValueCollection 并且确实想从原始集合中删除项目,那么是的,您必须在循环中执行此操作:

for (int i = 0; i<4; i++)
{ 
     myNameValueCollection.Remove(myNameValueCollection[i]);
}

请注意,您应该使用 NameValueCollection 的唯一原因 是指您绑定到 .NET 1.1 或更早版本。对于所有其他版本,相当于 NameValueCollection 的是 Dictionary

If you really want to use NameValueCollection and you really want to remove items form the original collection then yes, you have to do it in a loop:

for (int i = 0; i<4; i++)
{ 
     myNameValueCollection.Remove(myNameValueCollection[i]);
}

Note that the only reason you should use the NameValueCollection is when you are bound to .NET 1.1 or earlier. For all other versions the equivalent to NameValueCollection is Dictionary<string,string>

凡尘雨 2024-10-03 10:39:47

在 C# 字典中,或任何键值对集合(例如 NameValueCollection)没有 GetRange、AddRange 方法...
但是,如果您使用 Dictionary 而不是 NameValueCollection,则可以使用 Linq 查询来让您的生活更轻松。
您可以删除前 5 个元素,并且可以选择以“A”开头的名称:

        var myMap = new Dictionary<string, string>();
        var myMapFirst5 = myMap.Take(5);
        var myMapWithA = myMap.Select(x => x.Key.StartsWith("A"));

In C# Dictionary, or any Key-Value pair collection such as NameValueCollection don't have GetRange, AddRange methods...
However if you'd use Dictionary instead of NameValueCollection you could use Linq queries to make your life easier.
You can remove the first 5 elements, and you could select, for example, names starting with "A":

        var myMap = new Dictionary<string, string>();
        var myMapFirst5 = myMap.Take(5);
        var myMapWithA = myMap.Select(x => x.Key.StartsWith("A"));
狠疯拽 2024-10-03 10:39:47

乍一看,我会使用 LINQ 的 Take 方法来获取此类集合的前 5 个元素。不幸的是,linq 无法访问 NameValueCollection 对象。

这就是为什么我建议使用 List 或 SortedList 来代替:

using System.Linq;

var y = new SortedList<string, string>();
y.Add("Element1Key", "Element1Value");
y.Add("Element2Key", "Element2Value");
...
y.Add("Element20000Key", "Element20000Value"); // ;)
var MyFirstFive = y.Take(5);

如果您确实需要使用 NameValueCollection,那么您可以查看此相关问题,参考 使 NameValueCollection 可访问 LINQ。使用 LINQ 将使您的生活更轻松

At first sight, I would use the Take method of LINQ to get the 5 first elements of such a collection. Unfortunately, the NameValueCollection object is not accessible to linq.

That's why I would advice to use a List or a SortedList instead:

using System.Linq;

var y = new SortedList<string, string>();
y.Add("Element1Key", "Element1Value");
y.Add("Element2Key", "Element2Value");
...
y.Add("Element20000Key", "Element20000Value"); // ;)
var MyFirstFive = y.Take(5);

If you really need to use a NameValueCollection, then you can have a look on this related question that refer to make a NameValueCollection accessible to LINQ. Using LINQ will make your life easier

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