Compare listview items in two adjacent listviews, and do stuff with identical items… Taking too loooong

早过忘川 提交于 2020-01-06 18:10:59

问题


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

回答1:


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



回答2:


Probably you can try more efficient data structure. My VB.NET skills are very poor but here is a simple C# version.

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


来源:https://stackoverflow.com/questions/14628834/compare-listview-items-in-two-adjacent-listviews-and-do-stuff-with-identical-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!