I’m perhaps being a bit lazy asking this here, but I’m just getting started with LINQ and I have a function that I am sure can be turned into two LINQ queries (or one nested query) rather than a LINQ and a couple of foreach statements. Any LINQ gurus care to refactor this one for me as an example?
The function itself loops through a list of .csproj files and pulls out the paths of all the .cs files included in the project:
static IEnumerable<string> FindFiles(IEnumerable<string> projectPaths) { string xmlNamespace = '{http://schemas.microsoft.com/developer/msbuild/2003}'; foreach (string projectPath in projectPaths) { XDocument projectXml = XDocument.Load(projectPath); string projectDir = Path.GetDirectoryName(projectPath); var csharpFiles = from c in projectXml.Descendants(xmlNamespace + 'Compile') where c.Attribute('Include').Value.EndsWith('.cs') select Path.Combine(projectDir, c.Attribute('Include').Value); foreach (string s in csharpFiles) { yield return s; } } }
How about: