1

I need to be able to query a local directory for the files that I add to it. The method that contains the LINQ Query will return the file whose create date is the oldest. So, the order of operation is basically a First-In First-Out scenario.

Note that I am returning a list because the requirements may change to return more than one file.

The code I've come up with to achieve most of this is as follows:

public static List<FileInfo> GetNextFileToProcess(DirectoryInfo directory)
{
    var files = from f in directory.GetFiles()
                orderby f.CreationTime ascending
                select f;

    return files.Cast<FileInfo>().ToList();
}

The problem is that I have not limited this list to containing and returning only the file whose index is 0 after determining the sort order.

Do I need a where clause to limit the oldest file from being returned or what?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Isaiah Nelson
  • 2,450
  • 4
  • 34
  • 53
  • @JonSkeet Because my returned object is a List and an IEnumerable is not a list and so needs to be cast to one to return it. – Isaiah Nelson Feb 07 '12 at 19:57
  • That's why you're calling `ToList()`, but `Cast()` still just returns `IEnumerable`. – Jon Skeet Feb 07 '12 at 20:00
  • @JonSkeet AH! Thank you! I didn't change that code as before I was returning out a custom object. Damn. See, shotgunning code bites you in the butt always. – Isaiah Nelson Feb 07 '12 at 20:03

2 Answers2

3
var files = (from f in directory.EnumerateFiles()
             orderby f.CreationTime ascending
             select f).Take(1);
Tomalak
  • 332,285
  • 67
  • 532
  • 628
2

You've said you want to return a list in case you need more than one file later on - so why bother limiting it? You could use Take(1), but why not just return everything to the caller and let them decide what they want?

If you're going to limit it to "a list containing one element" you might as well make it just return a FileInfo instead - you'll need to modify the code if you want to return more than one entry anyway.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • This is true. I am dealing with some rolling requirements at the moment that will likely change by the time I have my meeting on Friday. – Isaiah Nelson Feb 07 '12 at 20:01
  • @Tomalak True, but there has been talk of processing two files at a time, so in that case I would need the two oldest files from the directory. I just wanted to see how I could handle this with a list moreover. – Isaiah Nelson Feb 07 '12 at 20:24
  • 2
    @fullNelson: Do you even need to return a `List` rather than an `IEnumerable`? If you just return the files in order, then the caller can decide whether to call `Take(1)` or `Take(2)`... – Jon Skeet Feb 07 '12 at 20:25
  • @JonSkeet Thats a good idea. Shaping it downstream would be a better option. – Isaiah Nelson Feb 07 '12 at 20:45
  • @fullNelson I concur with that. While my answer may have solved your immediate problem, Jon's is the better solution. – Tomalak Feb 07 '12 at 20:49