0

いくつかのバリエーションをリストする必要があります。最初のリストのサンプル

リスト1)

hkdhksa
OP-ID: 111112
jklfjdlkfsd
hfldhfjksdf
OP-ID: 111113
ghjg
OP-ID: 111114
OP-ID: 111115
gjgjhghgjhg
OP-ID: 111116
OP-ID: 111117
OP-ID: 111118

リスト 2)

OP-ID: 111112
OP-ID: 11113
OP-ID: 111114
OP-ID: 111115
OP-ID: 111117

結果は次のようになります: OP-ID: 11118 is not in List 2

Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    'Declare two dictionaries. The key for each will be 
    ' the text from the input line up to,
    'but not including the first ",". 
    ' The valus for each will be the entire input line.

    'Dim file1 As New HashSet(Of String) '!
    Dim file1 As New Dictionary(Of String, String)
    Dim file2 As New Dictionary(Of String, String)

    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        Dim part() As String = line.Split(",")

        If line = ("OP-ID: ") Like "OP-ID:*" Then
            If Not file1.ContainsKey(part(0)) Then file1.Add(part(0), line)
        End If

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",")
        If Not file2.ContainsKey(part(0)) Then file2.Add(part(0), line) '!
    Next


    Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys)
    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)

    txtResults.Text = ("IDs should not be in list: " & str)

End Sub
4

2 に答える 2

0

あなたのサンプルリストにはコンマがありません.それらが存在するかどうかは質問から明らかではありません.

カンマがない場合:

重複がない場合TEST1(または気にしない場合):

Dim hs As New Hashset(Of String)(File.ReadLines(TEST1))
hs.ExceptWith(File.ReadLines(TEST2))
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, hs)}"

重複が重要な場合:

Dim hs As New Hashset(Of String)(File.ReadLines(TEST2))
Dim notFound = File.ReadLines(TEST1).Where(Function(x) Not hs.Contains(x)).ToList
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

カンマがある場合

(共有コード)

Dim keyParser = Function(x As String)
        Dim indexOf = s.IndexOf(
        If indexOf = -1 Then Return x
        Return Left(x, indexOf)
    End Function
Dim list2Keys = New Hashset(Of String)(File.ReadLines(TEST2).Select(keyParser))

に重複キーがない場合TEST1:

Dim list1 = File.ReadLines(TEST1).ToDictionary(keyParser)
Dim notFound = list1.Where(Function(kvp) Not list2Keys.Contains(kvp.Key)).Select(Function(kvp) kvp.Value)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

に重複するキーがあるがTEST1、問題にならない場合:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).Select(Function(grp) grp.First)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"

の重複キーが重要な場合TEST1:

Dim list1 = File.ReadLines(TEST1).ToLookup(keyParser)
Dim notFound = list1.Where(Function(grp) Not list2Keys.Contains(grp.Key)).SelectMany(Function(grp) grp)
txtResults.Text = $"IDs not in list:{vbCrLf}{String.Join(vbCrLf, notFound)}"
于 2016-11-02T11:39:34.023 に答える