vb.net – 为什么LINQ中的Union函数不能删除重复的条目?

前端之家收集整理的这篇文章主要介绍了vb.net – 为什么LINQ中的Union函数不能删除重复的条目?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用VB .NET,我知道Union通常使用ByRef,但在VB中,字符串通常被处理为好像它们是原始数据类型.

因此,问题在于:

  1. Sub Main()
  2. Dim firstFile,secondFile As String(),resultingFile As New StringBuilder
  3.  
  4. firstFile = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\1.txt").Split(vbNewLine)
  5. secondFile = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\2.txt").Split(vbNewLine)
  6.  
  7. For Each line As String In firstFile.Union(secondFile)
  8. resultingFile.AppendLine(line)
  9. Next
  10.  
  11. My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\merged.txt",resultingFile.ToString,True)
  12. End Sub

1.txt包含:
一个
b
C
d
Ë

2.txt包含:
b
C
d
Ë
F
G
H
一世
Ĵ

运行代码后,我得到:
一个
b
C
d
Ë
b
F
G
H
一世
Ĵ

任何使联盟功能都像数学对应的建议?

Linq Union确实按照您的意愿执行.确保输入文件正确(例如,其中一行可能在换行符之前包含空格)或者在拆分后修剪字符串?
  1. var list1 = new[] { "a","s","d" };
  2. var list2 = new[] { "d","a","f","123" };
  3. var union = list1.Union(list2);
  4. union.Dump(); // this is a LinqPad method

linqpad中,结果是{“a”,“s”,“d”,“f”,“123”}

猜你在找的VB相关文章