【发布时间】:2018-11-23 06:47:50
【问题描述】:
我在使用 Linq 方面没有真正的经验,我正在努力理解 .ToHashSet。
我正在为此使用 VB。
我有这个代码:
' Import the list of Windows Classes to exclude; one per line.
Dim strRead As String = My.Computer.FileSystem.ReadAllText("C:\Exclude.txt")
' Split the string into a string array
Dim strExcludes As String() = strRead.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
' Convert the String Array to a HashSet (Traditional Method)
Dim lList As New HashSet(Of String)(strExcludes)
这按预期工作;我将文本文件中的每一行作为每个 Key 1 行放入 HashSet。
现在,我想看看如何使用.ToHashSet 来做同样的事情。下面的代码似乎可以正常工作,只是它返回类型为<Of Char>,所以我在它自己的 Key 中获取每个字符,而不是在它自己的 Key 中获取每一行。
' Convert the String Array to a HashSet (Using System.Linq Method) (Unfinished: Works, but needs to be converted from Char to String)
Dim lListLinq = strRead.ToHashSet()
我已经用谷歌搜索过,并与它进行了一些斗争,试图让它以 <Of String> 类型返回,但对于 VB 的 .ToHashSet 或在 C# 中关于这个特定的信息,没有太多谷歌信息问题。我没有看到在哪里进行转换; .ToHashSet 本身也不接受任何参数。
有人知道我错过了什么吗?我有一种感觉,这是一件非常简单的事情。
【问题讨论】: