I’ve a class that has a list of IDs with a function for adding new IDs from a list of objects.
I’ve got this resolved, but I’m sure this can be done with much less code.
Public Class Page Private _IdList As List(Of Integer) Private _HasNewItems As Boolean = False Public Sub AddItems(ByVal Items As List(Of Item)) Dim itemsID = From itemX In Items _ Select itemX.ID Dim list As List(Of Integer) = itemsID.ToList If _IdList.Intersect(list).Count = list.Count Then _HasNewItems = False Else _HasNewItems = True _IdList.AddRange(list) _IdList = _IdList.Distinct End If End Sub End Class
So how can this be done with less code and more from a NET 3.5 perspective…
Try the following
The trick here is that Except will return all items Except when they are present in the enumerable passed as the argument (In this case _IdList). So items.Except(_IdList) is all new items.