循环中重复QuestionName [英] Duplicate QuestionName in a loop

查看:103
本文介绍了循环中重复QuestionName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我将有一个循环。例如,我有两个不同的记录,在我的DtoList中说Hello和HelloWorld。但不知何故,Hello和HelloWorld会复制自己并且每个都有2条记录。

这意味着我的dtolist中共有4条记录。

因此,每次刷新tvQuestionList时,它将显示总共4条记录而不是2条记录。

如何更改下面的代码逻辑使其成为只显示2条记录?

以下是我的代码:



Hi, I have the following codes in which I will have a loop. For example, I have 2 different records, let say "Hello" and "HelloWorld" inside my DtoList. But somehow the "Hello" and "HelloWorld" will duplicate themselves and have 2 records for each off them.
Which means total of 4 records inside my dtolist.
Therefore, everytime the "tvQuestionList" is refreshed, it will displays total of 4 records instead of 2 records.
How can I change the codes logic below to make it become only 2 records to be displayed?
Below are my codes:

private void PopulateQuestionTree(bool isDelete)
{
    tvQuestionList.BindingContext = null;
    tvQuestionList.Nodes.Clear();
    CustomDataCollectionQuestionDtoList questionList = new CustomDataCollectionQuestionDtoList();
    questionList.DtoList = _customDataCollectionQuestionList.DtoList.Where(q =>
        q.TrackingState != BaseDto.TrackingInfo.Deleted).OrderBy(q => q.Sequence).ToList();
    for (int i = 0; i < questionList.DtoList.Count; i++)
    {  
         tvQuestionList.Nodes.Insert(i, CreateTreeNode(questionList.DtoList[i], i));       
    }
    tvQuestionList.Refresh();
}

推荐答案

private void PopulateQuestionTree(bool isDelete)
{
    tvQuestionList.BindingContext = null;
    tvQuestionList.Nodes.Clear();
    CustomDataCollectionQuestionDtoList questionList = new CustomDataCollectionQuestionDtoList();
    questionList.DtoList = _customDataCollectionQuestionList.DtoList.Where(q =>
        q.TrackingState != BaseDto.TrackingInfo.Deleted).OrderBy(q => q.Sequence).ToList();
    for (int i = 0; i < questionList.DtoList.Count; i++)
    {
           if(tvQuestionList.Nodes.Contains(x=>x.Name!=questionList.DtoList[i].Name)
         tvQuestionList.Nodes.Insert(i, CreateTreeNode(questionList.DtoList[i], i));
    }
    tvQuestionList.Refresh();
}


这篇关于循环中重复QuestionName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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