-2

see my post.

Here I am getting latest created file in each sub folder, but I need to get the latest created files with different extensions.

For example, there are five sub folders each containing more than one PDF file and more than one Excel file, then the query should bring one PDF file and one Excel file from each directory. I should have ten items in the result.

Community
  • 1
  • 1
Kuttan Sujith
  • 7,661
  • 18
  • 60
  • 89

1 Answers1

1

Would this work for you?

DirectoryInfo di = new DirectoryInfo(@"C:\Installers");

var recentFiles = di.GetDirectories()
    .SelectMany(d => d.EnumerateFiles()
                      .GroupBy(f => f.Extension)
                      .Select(gf => gf.OrderByDescending(f => f.CreationTimeUtc)
                                      .FirstOrDefault()))
    .Where(x => x != null)
    .Select(x => x.FullName)
    .ToList();
Seb Boulet
  • 931
  • 6
  • 18