0

我已经接近答案了,但很烦人,不完全是。到目前为止,我的代码是:

   private void lstIndividuals_SelectedIndexChanged(object sender, EventArgs e)
   {
        var individual = lstIndividuals.SelectedItem as Individual;
        var tempSimilarFilesToFile1 = new HashSet<Individual>();
        int Counter = 0;
        foreach (KeyValuePair<int, Individual> kvpInd in _Individuals1)
        {
            tempSimilarFilesToFile1 = new HashSet<Individual>();
            foreach (KeyValuePair<int, Individual> kvpInd2 in _Individuals2)
            {
                if (kvpInd.Value.name.name.ToLower() == kvpInd2.Value.name.name.ToLower())
                {
                    Counter++;
                    similarInds.Add(kvpInd.Value);
                    if (Counter >= 1)
                    {
                        tempSimilarFilesToFile1.Add(kvpInd2.Value);
                    }
                }
            }
        lstIndividuals2.DataSource = tempSimilarFilesToFile1.ToList();
        lstIndividuals2.DisplayMember = "DisplayName";
        lstIndividuals2.ValueMember = "id";
   }

正如您可能看到的,lstIndividuals2列表框项目的缩放速度非常快。我只想点击一个项目lstIndividuals

然后我希望显示找到的类似记录(任何遵守规则的记录kvpInd.value.name.name == kvpInd2.value.name.name

所有类似的项目,我想存储在tempSimilarFilesToFile1其中,并作为数据源lstIndividual2

如果我解释得不好,我深表歉意。

谢谢你。

4

1 回答 1

0

tempSimilarFilesToFile1您每次都在外部循环中初始化,因此您实际上会得到一个列表,_Individuals2其中包含与 中的最终项目相同的项目_Individuals1。只需尝试在外部循环中注释 init 语句,看看是否有帮助。

private void lstIndividuals_SelectedIndexChanged(object sender, EventArgs e)    
{
    var individual = lstIndividuals.SelectedItem as Individual;
    var tempSimilarFilesToFile1 = new HashSet<Individual>();
    int Counter = 0;
    foreach (KeyValuePair<int, Individual> kvpInd in _Individuals1)
    {
        // comment the statement below
        //tempSimilarFilesToFile1 = new HashSet<Individual>();
        foreach (KeyValuePair<int, Individual> kvpInd2 in _Individuals2)
        {
            if (kvpInd.Value.name.name.ToLower() == kvpInd2.Value.name.name.ToLower())
            {
                Counter++;
                similarInds.Add(kvpInd.Value);
                if (Counter >= 1)
                {
                    tempSimilarFilesToFile1.Add(kvpInd2.Value);
                }
            }
        }
    lstIndividuals2.DataSource = tempSimilarFilesToFile1.ToList();
    lstIndividuals2.DisplayMember = "DisplayName";
    lstIndividuals2.ValueMember = "id";
}
于 2012-04-05T05:46:32.927 回答