我正在使用此查询:
Dim FilesDirsQuery = From file In
_directory.EnumerateFileSystemInfos()
Select file.Name, file.Extension
它获取_directory中的所有文件和目录。但是如何通过扩展和名称来订购查询?
所以我发现我必须使用它来扩展订购:
Dim FilesDirsQuery = From f In
_directory.EnumerateFileSystemInfos()
Select f.Name, f.Extension Order By Function(f As IO.FileInfo)
Return f.Extension
End Function
我必须使用“ThenBy”,但它不能在我的例子中使用。所以我试过了:
FilesDirsQuery.OrderBy(Function(f As IO.FileInfo)
Return f.Extension
End Function).ThenBy(..)
但它说我的嵌套函数没有兼容的委托。那我该怎么办?
答案 0 :(得分:1)
这将为您提供目录,然后按字母顺序提供文件。
我在查询中使用了f
而不是file
,因为Visual Studio希望将file
更改为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
修改(因为问题从按类型顺序更改为按扩展名排序)
要按扩展程序订购,只需使用:
Dim FilesDirsQuery = From f In
_directory.EnumerateFileSystemInfos()
Select f.Name, f.Extension
Order By Extension, Name
编辑2: Lambda函数
你所谓的嵌套函数,实际上需要是一个Lambda函数。以下是使用Lambda调用OrderBy
方法的方法:
FilesDirsQuery.OrderBy(Function(f) f.Extension).ThenBy(..)
隐含参数类型和return语句。
答案 1 :(得分:0)
我不能在vb中说出来,但是如果你可以从c#进行转置,这可以帮助你:
var FilesDirsQuery = from fsInfo in _directory.EnumerateFileSystemInfos()
let type = fsInfo.GetType()
orderby type
orderby fsInfo.Name //or fsInfo.FullName
select new {fsInfo.Name, fsInfo.Extension};