Removing values from a generic list

64 views Asked by At

1.I have a generic List(OfContent) which represents all the content available for the user.

2.I have as well for each user an array of string which contains all the Content Ids that are already sent to the user.(The array is string because it is generated from a string.split method.)

Ex:Content Class

Public Class Content
    Public Property ContentId As Integer
    Public Property ContentType As Integer
    Public Property ContentToSend As String
    Public Property Lang As Integer
    Public Property EncryptedContentId As String


    Public Sub New()
        Me.ContentId = 0
        Me.ContentType = 0
        Me.ContentToSend = String.Empty
        Me.Lang = 0
        Me.EncryptedContentId = String.Empty
    End Sub

End Class

the string array that I have for ex:

Dim contentSent() As String = {"1", "3", "4", "9", "7"}

the elements 1,3,4.... in the above array represents logically the ContentId in my content Class.

my question is that what is the best way to filter out the contents of the array from the list(Of Content) that i have.

P.s I Am not limited with the choice of Generic-list i can use a data table instead if that helps with the performance. Currently we are having performance issues as I am using indexof() then remove at to filter out the contents.

some of the ways i have seen is - 1.Linq with lists - 2.linq with datatable

Update: This is the code i am using currently

Public Sub SendContent()

  Dim contentCollection As New List(Of Content)
  Dim SubscriberCollection As New List(Of Subscriber)


  'Fills the list Of Content
  contentCollection = CContent.GetRandomContentNotscheduled(MT, ClientId, SQLConnection_Cont)

  'Gets a List(Of Subscribers) which contains the Subscribers that the content should be sent to.
  SubscriberCollection = CContent.GetSubscribersContent(ClientId, MT, False, SQLConnection_Cont)

  For i As Integer = 0 To SubscriberCollection.Count - 1
      Dim index As Integer = 0
      Dim ContentSent() As String =  SubscriberCollection.ContentIdSent.Split(",")

      For j As Integer = 0 To ContentSent.Length - 1 Step 1
          Dim cntId As Integer = Convert.ToInt32(ContentSent(j))
          index = contentCollection.FindIndex(Function(c) c.CntId = cntId)
        contentCollection.RemoveAt(index)
      Next

  Next

End Sub
1

There are 1 answers

0
temarsden On

I think logically maybe you are looking for something like this...

VB.NET

    Dim allContent As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    Dim sentContent As Integer() = {1, 3, 4, 9, 7}

    Dim filteredContent = allContent.Where(Function(i) Not sentContent.Contains(i))

C#

    int[] allContent = {1,2,3,4,5,6,7,8,9};
    int[] sentContent = {1,3,4,9,7};

    var filteredContent = allContent.Where(i => !sentContent.Contains(i));

results

    //filteredContent would be 2, 5, 6, 8