Add to C # dictionary: cannot convert from 'string' to 'System.Collections.Specialized.NameValueCollection'

I have a data source that contains 3 different values, like below,

List<Configuration> lst = new List<Configuration> { new Configuration{Name="A", Config="X", Value="1"}, new Configuration{Name="A", Config="X", Value="2"}, new Configuration{Name="B", Config="Y", Value="2"} }; public class Configuration { public string Name { get; set; } public string Config { get; set; } public string Value { get; set; } } 

Here I want to iterate over the entire source and want to save the value of "Name" as KEY and "Config" and "Value" in "NameValueCollection".

To do this, I take the dictionary as shown below,

 var config = new Dictionary<string, NameValueCollection>(); 

But when adding to this dictionary, I run into 2 problems,

 foreach(var c in lst) { config.Add(c.Name, new NameValueCollection { c.Config, c.Value }); } 
  • Duplicate Key (Name = "A")
  • this line gives an error, new NameValueCollection {c.Config, c.Value});

Note. I want both 1 and 2 for X (in case of key duplication)

Is there any better C # collection or how to resolve the above error.

Thanks!

+7
c #
source share
4 answers

Try the following:

  List<Configuration> lst = new List<Configuration> { new Configuration{Name="A", Config="X", Value="1"}, new Configuration{Name="A", Config="X", Value="2"}, new Configuration{Name="B", Config="Y", Value="2"} }; var config = new Dictionary<string, NameValueCollection>(); NameValueCollection temp = new NameValueCollection(); foreach (var c in lst) { if(config.TryGetValue(c.Name, out temp)){ config[c.Name].Add(c.Config,c.Value); } else{ config.Add(c.Name, new NameValueCollection { {c.Config,c.Value } }); } } 
+3
source share

You can use the lookups dictionary (it is a collection where the key is mapped to multiple values):

 var config = lst.GroupBy(cfg => cfg.Name) .ToDictionary(g => g.Key, g => g.ToLookup(cfg => cfg.Config, cfg => cfg.Value)); 

Type of configuration will be

 Dictionary<string, ILookup<string, string>> 

Access to values:

 config["A"]["X"] // gives you IEnumerable<string> with values ["1","2"] 
+6
source share

A NameValueCollection contains several names and values, because their collection. Therefore, you cannot initialize it in your path. But you can use it to group all duplicates of Name into one collection. I would use Dictionary<k,v>.TryGetValue :

 var config = new Dictionary<string, NameValueCollection>(); foreach (Configuration c in lst) { NameValueCollection nvc; if(!config.TryGetValue(c.Name, out nvc)) { nvc = new NameValueCollection(); config.Add(c.Name, nvc); } nvc.Add(c.Config, c.Value); } 
+2
source share

try the following: in the dictionary class, there is no need to verify that the value is duplicated or not; if its first time will be added. if the second time it will be updated.

  List<Configuration> lst = new List<Configuration> { new Configuration{Name="A", Config="X", Value="1"}, new Configuration{Name="A", Config="X", Value="2"}, new Configuration{Name="B", Config="Y", Value="2"} }; var config = new Dictionary<string, NameValueCollection>(); foreach (var c in lst) { config[c.Name] = new NameValueCollection { { c.Config, c.Value } }; } 
0
source share

All Articles