0

I am coding on VB.net . I'like to compare 2list and find not matching items. But my code looks for only the first list items so it doesn't check if there is missing.

I will use Serie_Code in my comprasion .

SelectedItems is a List Of String,

SeriConrolList is a List:

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

And this is how 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 has 28items.

SelectedItems has 26items.

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

I've also tried this:

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

Also tried to reverse of this comparison. There's 2missing items and they return just 0.

Thank you

EDIT: I did it as the following and works fine:

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
Maestro00
  • 21
  • 9
  • Do the two missing items have anything in common, such as they are the only items with (or without) an "_" ? – Andrew Morton Apr 25 '19 at 08:19
  • Hi Andrew. There's nothing special for this two missing items, they are like the others. Thank you for interest. – Maestro00 Apr 25 '19 at 08:46
  • But there's no error, code works fine. It's a logical thing. Because when i delete "Not" part on ` Dim Result = (SelectedItems.Where(Function(x) Not hash.Contains(x.ToString))).ToList ` It takes all 28 items. – Maestro00 Apr 25 '19 at 09:07
  • Thank you but i use this list in many places and i will edit the part of SeriConrolList is a List Of an Object on the top and will try to explain more clearly. – Maestro00 Apr 25 '19 at 09:14
  • Maybe this helps: https://stackoverflow.com/questions/2404301/linq-find-differences-in-two-lists – Nostromo Apr 25 '19 at 11:20
  • They are not in the same type and not symmetric so that looks not possible. Thank you anyway... – Maestro00 Apr 25 '19 at 11:37
  • I also needed this but it's not possible, you can compare one list with each other of other list though, so that's kind of like it. – CruleD Apr 25 '19 at 13:13

0 Answers0