Trowa Trowa - 2 months ago 7
C# Question

Reading KeyValueConfigurationElement

I try to load the app settings of my self-defined setting tag but I got the following error:


Error 62 Cannot implicitly convert type
'System.Collections.Generic.IEnumerable'
to 'System.Configuration.KeyValueConfigurationElement[]'. An explicit
conversion exists (are you missing a cast?)


Do you have any idea how to solve this? The code I have is as follows:

KeyValueConfigurationElement[] test = (from KeyValueConfigurationElement e in ((AppSettingsSection)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).GetSection("mySection")).Settings
where e.Value == "1"
select e);


It highlighted the error at "where" in the code. I think this is quite simple but I can't solve it, tks.

Answer

You have to put .ToArray() to the end of your expression:

KeyValueConfigurationElement[] test = (from KeyValueConfigurationElement e in ((AppSettingsSection)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).GetSection("mySection")).Settings
        where e.Value == "1"
        select e).ToArray();
Comments