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!
c #
user584018
source share