You can use the OfType operator:
Filters IEnumerable elements based on the specified type.
foreach(var element in fileEnvironmentSection.FileEnvironmentList .OfType<FileEnvironmentElement>()) { result.Add(element.Key, element.Value); }
LINQ is not going to help with the body of the loop, as it mutates an existing dictionary. LINQ is typically used to create new data, not to modify existing data.
If you do not mind returning a new dictionary, you can do:
var newResult = fileEnvironmentSection.FileEnvironmentList .OfType<FileEnvironmentElement>() .ToDictionary(element => element.Key, element => element.Value);
source share