【问题标题】:Compare listview items in two adjacent listviews, and do stuff with identical items... Taking too loooong比较两个相邻列表视图中的列表视图项目,并使用相同的项目做一些事情......太长了
【发布时间】:2013-02-13 15:29:15
【问题描述】:

我正在比较两个相邻列表视图中的项目,并标记相关的项目(在我的列之一上,即产品 ID)。

我的问题是完成此过程所需的时间(几分钟)。我目前使用“finditemwithtext”和重载在子项中包含搜索(我的产品 id 列在子项(1)上...

我在列表视图 1 中有 12K 项,在列表视图 2 中有 6k 项。

目前我正在“逐步”浏览列表视图 1,并在列表视图 2 中搜索类似项目。

反过来说,遍历 2,在 1 中搜索,可能会遇到相同的性能问题,因为它只遍历 6k 个项目,但搜索 12k,vs 遍历 12k,搜索 6k...

也许有更有效的方法来获得最终结果?

当然,要比较的东西太多了……6000 x 6 列(36000 次比较)……根据我的微薄计算……

谢谢,不胜感激...

代码:

     Dim tmpListviewItem As New ListViewItem
     Dim c As Int32 = 0


     For Each i As ListViewItem In list1.Items

     If i.SubItems(5).Text = "" Then 'not yet linked item
     tmpListviewItem = list2.FindItemWithText(i.SubItems(1).Text, True, 0, False)

        If tmpListviewItem Is Nothing Then 'nothing found...

        Else 'found corresponding item
           c += 1
           i.SubItems(5).Text = tmpListviewItem.SubItems(1).Text
           tmpListviewItem.SubItems(5).Text = i.SubItems(1).Text
           i.ForeColor = Color.Green
           tmpListviewItem.ForeColor = Color.Green

       End If
     End If
     Next

【问题讨论】:

  • 在评论中,我将尝试手动遍历第二个列表视图,并在找到项目时执行当前搜索..并且只搜索一个子项目..但不确定这将获得多少性能......我不确定“findiitemwithtext”方法内部会发生什么......
  • 您要搜索的子项在每个列表中是否唯一?我会使用 KeyedCollection 并有一个颜色属性。并将 ListViews 绑定到 KeyedCollection。
  • 嗨,好的,必须对此进行一些研究......是的,产品代码子项“应该”是唯一的......它们直接来自两个不同的数据库......其中我相信他们是PK。
  • 那么肯定是 KeyedCollection 或 Dictionary。使产品 ID 成为关键。使用字典,您可以重复产品 ID 作为键和值中的属性。使用 KeyedCollection,您不必复制密钥。

标签: c# .net vb.net listview compare


【解决方案1】:

好吧,简单来说,我所做的,是这样的:

我构建了一个自定义对象类型的自定义列表,其中仅存储代码和数量信息(在我的例子中)。数量是我的列表视图中的一列。查找比较时,我需要它做一些额外的事情。

1:我在最初加载“更大”列表视图时使用对象构建列表(内存中的纯对象,没有接口绑定)

2:我的两个 lsitview 都在代码字段中排序

3:我的自定义列表显然也是这样排序的

4:然后,我逐步浏览较小的列表视图,并逐步浏览完整的自定义列表,进行比较。找到比较后,我退出自定义列表,然后从自定义列表中删除该对象,以不断缩小我的列表。

5:由于我对代码字段进行排序,因此始终可以在自定义列表的几百次迭代中找到对象。

这使我的比较方法从大约 10 分钟缩短到 10 多秒。

    Private Function _find_item_in_rev(itemCode As String) As xStockitem
    Dim myTempItem As New xStockitem
    Debug.Print(currentRevItems.Count.ToString)

    For Each thisItem As xStockitem In currentRevItems

        If thisItem.stockCode = itemCode Then 'found item
            myTempItem.stockCode = itemCode
            myTempItem.price = thisItem.price
            myTempItem.quantity = thisItem.quantity
            currentRevItems.Remove(thisItem)
            Return myTempItem
        End If

    Next
    Return Nothing 'nothing found
End Function

【讨论】:

    【解决方案2】:

    或许你可以尝试更高效的数据结构。我的 VB.NET 技能很差,但这里有一个简单的 C# 版本。

    var dict2 = new Dictionary<string, ListViewItem>();
    
    foreach (ListViewItem item in list2.Items)
    {
        dict2.Add(item.SubItems["ProductId"].Text, item);
    }
    
    foreach (ListViewItem item in list1.Items)
    {
        var productId = item.SubItems["ProductId"].Text;
    
        ListViewItem item2;
        if (dict2.TryGetValue(productId, out item2))
        {
            // TODO:
            item2.ForeColor = Color.Green;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      相关资源
      最近更新 更多