Click here to Skip to main content
15,917,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

I use a LINQ query from another forum discussion and was able to adapt it, so that it works and even reports activity to the UI (see code below). The result of the query is a BindingList(of FileInfo), which I then use as DataSource of a "FilesBindingSource" in order to feed a DataGridView.

Now I'd like to retrieve / display only a part of the information (FileName + Length + LastWriteTime), and I read that I wanna put another "Select" after the Where-clause... but I've been struggling with the syntax all day long.

Could someone please help me with the correct syntax?

Thank you very much,
Kind regards

What I have tried:

For Each result As FileInfo In dirInfo.GetFiles(pattern).Select(Function(file) file).Where(Function(s)
Next result
               Function(d) Task.Factory.StartNew(
			   Sub() InternalCollectFiles(d, pattern, queue))).ToArray())
Updated 4-Feb-19 3:10am

Here is the Linq required to do what you want:
Dim results = dirinfo.GetFiles(pattern)
    .Where(Function(x) supportedExtensions.Contains(x.Extension.ToLower()))
    .Select(Function(x) New With {
        .Name = x.Name,
        .Length = x.Length,
        .LastWriteTime = x.LastWriteTime

To help explain the Linq statement, less the selection of output, here is a non-Linq version:
Dim dirinfo = New DirectoryInfo(dirPath)
Dim files = dirinfo.GetFiles(pattern)

For Each fileInfo In files

    If supportedExtensions.Contains(fileInfo.Extension) Then
    End If
Share this answer
Maciej Los 4-Feb-19 9:15am    
You've been faster than me, because i had a debate with OP about his other issue.
Sonhospa 4-Feb-19 10:25am    
Thank you though, as from the "pupil's" perspective I understand more from your additional explanations. I also appreciate your very good (!) idea to return a specific type.
Graeme_Grant 4-Feb-19 10:32am    
Updated my solution with a non-Linq version...
Sonhospa 4-Feb-19 11:02am    
Good that you write, now I have a 'Reply' button so that I can thank you as well :-) and apologise for not being used to (and also comfortable with) the changed UI.

Thank you as well for the update / non-Linq version! It's obviously a very cool and brief way to reach the same goal without all the syntax hassle that I unsuccessfully tried to master. And in my learning, the different ways pros go are extremely interesting to me!

Would you think the LINQ version is preferrable for speed or sth else, or is it just a matter of taste?
Graeme_Grant 4-Feb-19 19:39pm    
No problems.

The Linq version above is delayed execution, so the code only runs when you iterate over the results. To force it to execute immediately, use .ToList(). Very powerful and flexible when used correctly...
It's quite easy. See:

Dim dirinfo As DirectoryInfo = New DirectoryInfo("C:\users\Sonhospa\documents") 'change path to your needs
Dim pattern As String = "*"
Dim supportedExtensions As String() = {".xls", ".doc", ".xlsx", ".docx"}

Dim filelist = dirInfo.GetFiles(pattern) _
    .Where(Function(file) supportedExtensions.Contains(Path.GetExtension(file.Extension).ToLower())) _
    .Select(Function(file) New With _
    { _
        .FileName = file.FullName, _
        .Length = file.Length, _
        .LastWriteTime = file.LastWriteTime _
    }) _

Note #1: i've moved Where statement up, before Select
Note #2: above code returns List(of AnonymousType(Of String, Int64, DateTime))

If you would like to return a colection of specific type, please see: How to: Return a LINQ Query Result as a Specific Type (Visual Basic) | Microsoft Docs[^]

For example:
Public Class ShortFileInfo

	Public Property FileName As String
	Public Property Length As Long
	Public Property LastWriteTime As DateTime
End Class

A query have to be changed this way:
.Select(Function(file) New ShortFileInfo() With _
{ _
    .FileName = file.FullName, _
    .Length = file.Length, _
    .LastWriteTime = file.LastWriteTime _
}) _
Share this answer
Sonhospa 4-Feb-19 10:31am    
Thank you again, Maciej. I guess I'll try the way with the extra class 'ShortFileInfo', since with the anonymous type I get an error (can't convert to FileInfo).

By the way your additional explanation helped me a lot!
Maciej Los 4-Feb-19 12:10pm    
You're very welcome.
I'm glad i can help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900