I tried to replace code
foreach (var discovery in mpwrapper.parser.Discoveries)
{
solution.AddFile("Discoveries", discovery.DisplayStringName + ".mpx", discovery);
}
with the following linq expression
mpwrapper.parser.Discoveries.Select(
s => solution.AddFile("Discoveries", s.DisplayStringName + ".mpx", s));
But got an error
The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
How to convert this foreach loop to linq query where I execute a method on each object in my IEnumerable collection?
solution.AddFilemethod doesn't return anything - would that be correct?Selectto execute code with side effects is improper use. Just do theforeach. OrList<T>.ForEach()which is a method that is expected to have side effects (= state changes outside the method).foreachloops to Linq just for the sake of it is one of the most common techniques to make code harder to understand and debug that I'm aware of in .NET.