2

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

Musa Hafalir
  • 1,752
  • 1
  • 15
  • 23
lyborko
  • 2,571
  • 3
  • 26
  • 54
  • So, you need a way to remove items from NameValueCollection like Remove(int startIndex, int count); right? – Musa Hafalir Oct 06 '10 at 06:51
  • Hm, similary... just simple, like setting length of an array (if I try to guess it intuitively): int[] arr2 = new int[5]; – lyborko Oct 06 '10 at 07:00

4 Answers4

0

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
  • 3,692
  • 3
  • 33
  • 46
0

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
  • 48,890
  • 37
  • 186
  • 278
  • 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 Oct 06 '10 at 07:32
  • Using Dictionary you could use Linq. Note however that Linq never modifies the orignal collection instance. – bitbonk Oct 06 '10 at 07:53
0

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

Community
  • 1
  • 1
Larry
  • 17,605
  • 9
  • 77
  • 106
  • 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. – Larry Oct 06 '10 at 09:32
0

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
  • 3,749
  • 1
  • 30
  • 54
  • Yes, i see some advantages, but I was curious how to accomplish this genuinely simple task... (or maybe from the first sight) – lyborko Oct 06 '10 at 07:46
  • it's simple (with LINQ) for common collections such as dictionary. Not so simple for less common collections :-) – Amittai Shapira Oct 06 '10 at 08:18