比较两个相邻列表视图中的列表视图项,并使用相同的项进行处理. [英] Compare listview items in two adjacent listviews, and do stuff with identical items... Taking too loooong

查看:88
本文介绍了比较两个相邻列表视图中的列表视图项,并使用相同的项进行处理.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的问题是他完成该过程所花费的时间(几分钟).我目前使用"finditemwithtext"和重载将searhces包含在子项中(我的产品ID列位于subitems(1)...

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

目前,我正在逐步"通过列表视图1,并在列表视图2中搜索类似的项目.

相反,逐步执行2,在1中进行搜索,可能会遇到相同的性能问题,因为它仅逐步执行6k项,但是搜索12k,而逐步执行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

解决方案

好吧,简单地说,我所做的就是:

我构建了一个自定义对象类型的自定义列表,该列表仅存储代码和数量信息(在我的情况下).数量是我的列表视图中的一列.寻找比较时,我需要它做一些额外的工作.

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

2:我的两个lsitviews都在代码字段上排序

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

I am comparing items in two adjacent listviews, and marking items that are related (on one of my columns, i.e. product ID).

My issue is with he time it takes to complete this process (several minutes). I currently use "finditemwithtext" and overloading to include searhces in subitems (my proiduct id column is on subitems(1) ...

I have 12K items in listview 1, and 6k items in listview 2.

Currently I am "stepping" through listview 1, and searching for a like item in listview 2.

Doing it the other way around, stepping through 2, searching in 1, would probably have the same performance issue, as its only stepping through 6k items, but searching 12k, vs stepping through 12k, searching 6k...

Maybe there is a more efficient way of getting to the end result?

Granted, its a heck load of stuff to compare... 6000 x 6 columns (36000 comparisons).. by my meager calculation...

Thanks, would appreciate some input...

Code:

     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

解决方案

Ok, simply put, what I have done, is this:

I build a custom list of a custom object type, which stores just the code, and quantity information (in my case). Quantity is a column in my listviews. I need it to do some extra stuff when finding a comparison.

1: I build the list with objects as I initially load the "bigger" listview (pure object in memory, no interface bound)

2: Both my lsitviews are ordered on the code field

3: My custom list is then obviously also ordered this way

4: I then step through my smaller listview, and step through the complete custom list, comparing. I exit the custom list when a comparison is found, and remove that object from the custom list, as to continually shrink my list.

5: Objects are always found within a couple of hundred iterations thorugh the custom list, because of my ordering on the code fields.

This has brought down my comparison method from about 10 minutes, to just over 10 seconds.

    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

这篇关于比较两个相邻列表视图中的列表视图项,并使用相同的项进行处理.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆