Order by fileinfo extension and name

I'm using this query:

Dim FilesDirsQuery = From file In
                   _directory.EnumerateFileSystemInfos()
                   Select file.Name, file.Extension

It gets all files and directories in the _directory. But how to order the query by extension and name?

So I found that I have to use this to order by extension:

Dim FilesDirsQuery = From f In
        _directory.EnumerateFileSystemInfos()
        Select f.Name, f.Extension Order By Function(f As IO.FileInfo)
                                              Return f.Extension
                                            End Function

And I have to use "ThenBy", but it can't be used in my example. So I tried:

FilesDirsQuery.OrderBy(Function(f As IO.FileInfo)
                        Return f.Extension
                       End Function).ThenBy(..)

But it say's that my nested function doesn't have a compatible delegate. So what should I do?

Answers


This will give you directories, then files alphabetically.

I used f instead of file in the query because Visual Studio wanted to change file to File.

Dim FilesDirsQuery = From f In
                     _directory.EnumerateFileSystemInfos()
                     Select f.Name, f.Extension, isDirectory = ((f.Attributes And FileAttributes.Directory) = FileAttributes.Directory)
                     Order By isDirectory Descending, Name

Edit: (because question changed from order by type to order by extension)

To order by extension, just use this:

Dim FilesDirsQuery = From f In
                     _directory.EnumerateFileSystemInfos()
                     Select f.Name, f.Extension
                     Order By Extension, Name

Edit 2: Lambda functions

What you're calling a nested function, actually needs to be a Lambda function. Here's how the OrderBy method would be called using a Lambda:

FilesDirsQuery.OrderBy(Function(f) f.Extension).ThenBy(..)

The parameter type and return statement are implied.


Need Your Help

jQuery Cross-site Call To ASMX service Return value null

jquery html ajax asmx cross-site

I am trying to call a asmx service from an HTML page through JavaScript and jQuery.

How do I bind a success behaviour to another ajax form submission?

ajax jquery

Similar to this question, I'm wanting to run a function when an ajax form is successfully submitted, but I don't have direct access to the initial ajax code that loads the form.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.