There are different syntaxes for using LINQ.
The first one looks a little bit like SQL (From x In y) and I'm not familiar with it.
The other one looks like common functions on collections. These functions will be executed from left to right, so your not working example
Directory.EnumerateFiles(root).Take(50).OrderByDescending(Of String)
would first execute EnumerateFiles
. Then it would take the first 50 items from the result of EnumerateFiles
and then it would sort these first 50 items in descending order.
Since you want it the other way around (first sort descending by date, then take the first 50 items) you have to tweak your code a little bit.
Dim root As String = "C:\Test"
Dim files As IEnumerable(Of String) = IO.Directory.EnumerateFiles(root) _
.OrderByDescending(Of String)(Function(x As String) x) _
.Take(50)
Right now it would sort the files by name in descending order. If you like to sort by other criteria, you have to change the OrderByDescending
part of the code, for instance like
....OrderByDescending(Of Date)(Function(x As String) IO.File.GetLastAccessTime(x))...
I hope this helps.