【问题标题】:Removing values from a generic list从通用列表中删除值
【发布时间】:2015-06-26 15:07:34
【问题描述】:

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

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

例如:内容类

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.... 逻辑上代表我的content Class 中的ContentId

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

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

我见过的一些方法是 - 1.Linq 与列表 - 2.linq 与数据表

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

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

【问题讨论】:

  • @james 我已经添加了包含当前行为的子。

标签: arrays vb.net linq generic-list


【解决方案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

【讨论】:

    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2022-09-29
    • 2013-04-12
    相关资源
    最近更新 更多