当 LINQ IEnumerable 的 return 为空时,如何不添加到列表中?
How to not add to the list, when the return of a LINQ IEnumerable is empty?
我有以下 VB.NET 代码:
Dim list = directoryQuery.Select(
Function(d) New With {
.dir = d.FullName,
.acl = GetFileSystemAccessRule(d).Select(
Function(a) New With {.if = a.Reference.ToString()}
)
}
)
End Sub
有时 GetFileSystemAccessRule(d).Select
的 return 是 Return Enumerable.Empty(Of FileSystemAccessRule)()
。在那种情况下,我既不希望向 list
添加 .directory
也不添加 .acl
。我想跳过它。
所以我考虑了之后删除空项目的选项。
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Enumerable.Empty(list)).ToList()
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Nothing).ToList()
可惜都失败了。我做错了什么?
我认为这是可行的方法:
list = list.Where(Function(a) a.acl.Any())
或类似的东西(我不太精通 VB.NET 语法)。
我有以下 VB.NET 代码:
Dim list = directoryQuery.Select(
Function(d) New With {
.dir = d.FullName,
.acl = GetFileSystemAccessRule(d).Select(
Function(a) New With {.if = a.Reference.ToString()}
)
}
)
End Sub
有时 GetFileSystemAccessRule(d).Select
的 return 是 Return Enumerable.Empty(Of FileSystemAccessRule)()
。在那种情况下,我既不希望向 list
添加 .directory
也不添加 .acl
。我想跳过它。
所以我考虑了之后删除空项目的选项。
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Enumerable.Empty(list)).ToList()
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Nothing).ToList()
可惜都失败了。我做错了什么?
我认为这是可行的方法:
list = list.Where(Function(a) a.acl.Any())
或类似的东西(我不太精通 VB.NET 语法)。