Reputation: 785
I am trying to run this line of code:
Dim OrderedFiles() As String = Directory.GetFiles(FilePath).OrderBy(x >= x.CreationTime)
I get an error on x saying x is not declared.
I have my project set to Option Strict Off and Option Infer On. If I turn ON Option Strict then I get thousands of errors from the project(it is inherited) and I don't have the time to fix all of them, but x no longer gives me an error. I have googled until I want to throw my computer out the window.
Any help on how to correct this statement would be appreciated.
Edit:
I was hoping for a more elegant solution but here is what I came up with to solve this particular problem.
Dim fileList() As String = Directory.GetFiles(FilePath)
Dim fileDate(fileList.Length - 1) As DateTime
For i As Integer = 0 To fileList.Length - 1
fileDate(i) = New FileInfo(fileList(i)).CreationTime
Next
Array.Sort(fileDate, fileList)
With EmailTemplates_DropDownList
.DataSource = fileList.Reverse.Take(5)
.DataBind()
End With
It is not particularly elegant but it does the job. I was hoping for a one liner LINQ solution and I just don't have the background in LINQ to know how to do the job, time to go buy a book.
Upvotes: 2
Views: 1378
Reputation: 1500065
I'm not sure where you got this syntax from:
OrderBy(x >= x.CreationTime)
That almost looks like the C# syntax of
OrderBy(x => x.CreationTime)
but I believe in VB you would use
OrderBy(Function(x) x.CreationTime)
That's certainly what the example in Enumerable.OrderBy
would suggest.
EDIT: At that point you'll get a different error, as per Steve's post... but he hasn't corrected the syntax. I suspect you want:
Dim OrderedFiles() As FileInfo = new DirectoryInfo(FilePath).GetFiles().
OrderBy(Function(x) x.CreationTime).
ToArray()
Upvotes: 5
Reputation: 216273
Directory.GetFiles() returns the names of files (including their paths) in the specified directory. It's not possible to use x.CreationTime, x is a string
Probably you should use DirectoryInfo
Dim di as DirectoryInfo = new DirectoryInfo(FilePath)
Dim OrderedFiles = di.GetFiles().OrderBy(Function(x) x.CreationTime).Take(3)
Dim fi as FileInfo
For each fi in OrderedFiles
Console.WriteLine(fi.FullName)
Next
Upvotes: 2