Remove single value from NameValueCollection

My data source may have duplicate keys with values.

typeA : 1 typeB : 2 typeA : 11 

I decided to use NameValueCollection , because it allows you to enter duplicate keys.

I want to remove a specific key pair \ value from the collection, but NameValueCollection.Remove(key) removes all the values ​​associated with the specified key.

  • Is there a way to remove a single key \ value NameValueCollection from NameValueCollection , OR
  • Is there a better collection in C # that matches my data.

[EDIT 1]

Firstly, thanks for all the answers :)

I think I should have mentioned that my data source is XML.

I used System.Xml.Linq.XDocument to query the type, and it was also convenient to delete a specific value.

Now, my question is, for big data data, does XDocument use a good choice considering performance? If there are no other alternatives (maybe go back to NameValueCollection and use one of the methods mentioned to delete data)

+4
source share
6 answers

The idea of ​​storing multiple values ​​with the same key is something strange. But I think that you can get all the values ​​using GetValues, and then delete the one you don't need and put it back using Set and then the following Add methods. To do this, you can make a separate extension method method.

+2
source

NameValueCollection really does not allow you to have multiple records with the same key. It simply combines the new values ​​of existing keys into a comma-separated list of values ​​(see NameValueCollection.Add .

Thus, there really is one value for each key. Perhaps you can get the value dividing them by "," and remove the offensive value.

Edit: @ElDog is correct, there is a GetValues method which does this for you so no need to split.

The best option I consider using Dictionary<string, IList<int>> or Dictionary<string, ISet<int>> to store values ​​as discrete erm values

+1
source

You can convert it to hashtable

  var x = new NameValueCollection(); x.Add("a", "1"); x.Add("b", "2"); x.Add("a", "1"); var y = x.AllKeys.ToDictionary(k => k, k=>x[k]); 
0
source

create your own method, it works for me -

 public static void Remove<TKey,TValue>( this List<KeyValuePair<TKey,TValue>> list, TKey key, TValue value) { return list.Remove(new KeyValuePair<TKey,TValue>(key,value)); } 

then name it on the list as -

 list.Remove(key,value); //Pass the key value... 
0
source

Perhaps not the best way, but ....

 public class SingleType { public string Name; public int Value; } List<SingleType> typeList = new List<SingleType>(); typeList.Add (new SingleType { Name = "TypeA", Value = 1 }); typeList.Add (new SingleType { Name = "TypeA", Value = 3 }); typeList.Remove (typeList.Where (t => t.Name == "TypeA" && t.Value == 1).Single()); 
0
source

Instead, you can use the Dictionary collection:

 Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("typeA", 1); dictionary.Add("typeB", 1); 

When you try to insert type: 11 , it will throw an exception, since Key already exists. Thus, you can enter a new key to insert this data.

Refer to the Tutorial for more help.

-1
source

Source: https://habr.com/ru/post/1481381/


All Articles