比较两个列表查找不匹配的项目

时间:2019-04-25 07:59:26

标签: vb.net list linq

我正在VB.net上编码。我想比较2list并找到不匹配的项目。 但是我的代码仅查找第一个列表项,因此不会检查是否缺少列表项。

我会在使用Serie_Code的时候使用它。

SelectedItems是一个字符串列表,

SeriConrolList是一个列表:

Private Property SeriControlList As List(Of SeriRow)
    Get
        If Session("SeriControlList") Is Nothing Then
            Session("SeriControlList") = New List(Of SeriRow)
        End If
        Return Session("SeriControlList")
    End Get
    Set(value As List(Of SeriRow))
        Session("SeriControlList") = value
    End Set
End Property

这就是SeriRow:

Private Class SeriRow
    Public Serie_Code As String = String.Empty
    Public Serie_Name As String = String.Empty
    Public Serie_Name_Eng As String = String.Empty
    Public Frequency As String = String.Empty
    Public Behaviour As String = String.Empty
    Public Summable As Integer = 0
    Public Data_Source As String = String.Empty
    Public Data_Source_Eng As String = String.Empty
    Public Start_Date As Date? = Nothing
    Public End_Date As Date? = Nothing
End Class

SeriControlList有28个项目。

SelectedItems有26个项目。

Dim hash As HashSet(Of String) = New HashSet(Of String)(SeriControlList.Select(Function(x) x.Serie_Code.Replace("_", ".")))
Dim Result = (SelectedItems.Where(Function(x) Not hash.Contains(x.ToString))).ToList

我也尝试过:

Dim TupleAdd = (From nt In SeriControlList Where Not SelectedItems.Any(Function(n) Not nt.Serie_Code.Replace("_", ".") = n) _
              Select nt.Serie_Code, nt.Serie_Name, nt.Serie_Name_Eng, nt.Frequency, nt.Start_Date, nt.End_Date, _
              nt.Behaviour, nt.Data_Source, nt.Data_Source_Eng, nt.Summable).ToList

也试图扭转这种比较。有2个缺失项,它们仅返回0。

谢谢

编辑:我按照以下说明进行操作,并且工作正常:

For i = 0 To Me.SeriControlList.Count - 1
                If Not SelectedItems.Contains(SeriControlList(i).Serie_Code.Replace("_", ".")) Then
                    Tuple = New SeriRow
                    Tuple.Serie_Code = Me.SeriControlList(i).Serie_Code
                    Tuple.Serie_Name = Me.SeriControlList(i).Serie_Name
                    Tuple.Serie_Name_Eng = Me.SeriControlList(i).Serie_Name_Eng
                    Tuple.Start_Date = Me.SeriControlList(i).Start_Date
                    Tuple.End_Date = Me.SeriControlList(i).End_Date
                    Tuple.Frequency = Me.SeriControlList(i).Frequency
                    Tuple.Summable = Me.SeriControlList(i).Summable
                    Tuple.Behaviour = Me.SeriControlList(i).Behaviour
                    Me.TCMBtoVRMT.Add(Tuple)
                End If

            Next

0 个答案:

没有答案