0

I am using the following example from Stackoverflow:
How to find the oldest Folder in a Folder vb.net

Dim dirPrograms As New DirectoryInfo("c:\program files")

' LINQ query for oldest directory
Dim dir = (From dir In dirPrograms.EnumerateDirectories()).Min(function (o) o.CreationTime).FirstOrDefault()

I get the following error: Range variable 'dir' hides a variable in an enclosing block or a range variable previously defined in the query expression.

No one else is complaining about an error so I assume I must be doing something wrong.

1 Answer 1

1

Do this:-

Dim directoryInfo = (From dir In dirPrograms.EnumerateDirectories())
                        .Min(function (o) o.CreationTime)

Your dir declared variable (where you are storing the result) is same as your range variable dir in query-From dir In dirPrograms.. so compiler is complaining as these names will be obviously confusing between the two, So you need to change either of the two names.

Apart from this, You should not use FirstOrDefault after a Min as Min method will return a single value and not IEnumerable.

Sign up to request clarification or add additional context in comments.

2 Comments

I had already tried that thinking it might help, but it gives me this error instead: 'FirstOrDefault' is not a member of 'Date'
@rerat - That's obviously a different error. Min will return just a single value and not IEnumerable so no need of 'FirstOrDefault'. Please check my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.