从通用列表中删除值

时间:2015-06-26 15:07:35

标签: arrays vb.net linq generic-list

1.我有一个通用列表(OfContent),代表用户可用的所有内容。

2.我也为每个用户提供一个字符串数组,其中包含已发送给用户的所有内容ID。(该数组是字符串,因为它是从string.split方法生成的。)

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

我以前的字符串数组:

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

上面数组中的元素1,3,4 ....逻辑上代表ContentId中的content Class

我的问题是,从我所拥有的列表(Of Content)中过滤出数组内容的最佳方法是什么。

P.s我不受通用列表选择的限制我可以使用数据表,如果这有助于提高性能。目前我们遇到性能问题,因为我使用indexof()然后删除at以过滤掉内容。

我见过的一些方法是   - 1.Linq列表   - 2.linq with datatable

更新:这是我目前使用的代码

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 个答案:

答案 0 :(得分:1)

我认为逻辑上也许你正在寻找这样的东西......

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));

结果

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