views:

70

answers:

4

Hi,

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

A: 

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>>?

Andre Luus
in this context List has a method RemoveRange, which is similar to what I expect... :-)
lyborko
A: 

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>

bitbonk
finally, I did it similary as you wrote. But I am affraid, that using Dictionary will not solve my problem in the simpler way. I have to do it in the loop anyway. Thanx for the good tip, I did not know anything about Dictionary.
lyborko
Using Dictionary<string,string> you could use Linq. Note however that Linq never modifies the orignal collection instance.
bitbonk
A: 

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

controlbreak
looks fine, but y.Take(5); does not exist...
lyborko
my bad: you have to put "using System.Linq;" at the top, otherwise neither it wont recognize the .Take(5), nor it will suggest it with intellisense. "Add" also work better than "add" so I edit this also.
controlbreak
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"));
Amittai Shapira
Yes, i see some advantages, but I was curious how to accomplish this genuinely simple task... (or maybe from the first sight)
lyborko
it's simple (with LINQ) for common collections such as dictionary. Not so simple for less common collections :-)
Amittai Shapira