C# 比较两个相邻listview中的listview项,并使用相同的项执行操作。。。吃得太多

C# 比较两个相邻listview中的listview项,并使用相同的项执行操作。。。吃得太多,c#,.net,vb.net,listview,compare,C#,.net,Vb.net,Listview,Compare,我正在比较两个相邻列表视图中的项目,并标记相关的项目(在我的一列上,即产品ID) 我的问题是完成这个过程所需的时间(几分钟)。我目前使用“finditemwithtext”和重载在子项中包含searhces(我的proiduct id列在子项(1)上) 我在listview 1中有12K个项目,在listview 2中有6k个项目 目前,我正在“单步”浏览listview 1,并在listview 2中搜索类似的项目 如果换一种方式来做,单步执行2,在1中搜索,可能会有相同的性能问题,因为它只单

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

我的问题是完成这个过程所需的时间(几分钟)。我目前使用“finditemwithtext”和重载在子项中包含searhces(我的proiduct id列在子项(1)上)

我在listview 1中有12K个项目,在listview 2中有6k个项目

目前,我正在“单步”浏览listview 1,并在listview 2中搜索类似的项目

如果换一种方式来做,单步执行2,在1中搜索,可能会有相同的性能问题,因为它只单步执行6k项,但是搜索12k,而不是单步执行12k,搜索6k

也许有一种更有效的方法来达到最终的结果

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

谢谢,希望您能提供一些意见

代码:


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

var dict2=新字典();
foreach(list2.Items中的ListViewItem项)
{
添加(项。子项[“ProductId”]。文本,项);
}
foreach(list1.Items中的ListViewItem项)
{
var productId=item.SubItems[“productId”].Text;
ListViewItem2;
if(dict2.TryGetValue(productId,out item2))
{
//待办事项:
item2.ForeColor=颜色。绿色;
}
}

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

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

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

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

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

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

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

在注释中,我将尝试手动迭代第二个listview,并在找到一个项时执行当前搜索..并且仅搜索一个子项..但不确定这将获得多少性能..我不确定“finditemwithtext”中会发生什么方法…您正在搜索的子项是否在每个列表中都是唯一的?我将使用KeyedCollection并具有颜色属性。并将ListView绑定到KeyedCollection。您好,我们需要对此进行一些研究…是的,产品代码子项“应该”保持唯一性…它们直接从两个不同的数据库派生。我确定它们是PK。然后确定KeyedCollection或Dictionary。将product ID设置为键。使用Dictionary可以重复product ID作为键和值中的属性。使用KeyedCollection不必重复键。
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;
    }
}
    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