繁体   English   中英

在C#中的列表中查找项目

[英]Finding an item in a list in c#

下面的代码包含一个foreach循环,该循环遍历包含XML的字符串集合列表。 在枚举集合时,它将读取问题和答案元素并将其添加到列表集合。 我需要确保没有重复的问题添加到列表集合。

questionnaire.QuestionAnswers.Add(fataQuestionsAnswers)下面的代码questionnaire.QuestionAnswers.Add(fataQuestionsAnswers)将元素添加到列表集合。 我面临的问题是重复的问题已添加到列表中。 我试图提出以下条件:

if (questionnaire.QuestionAnswers.Find(a => a.Question != fataQuestionsAnswers.Question) == null) 

但这似乎不起作用。

var fataQuestionnaireData = DbAccess.GetFatcaQuestionnaire(contactId);
if (fataQuestionnaireData != null)
{
    var questionnaire = new FatcaQuestionnaire();

    foreach (var fatcaQuestionnaire in fataQuestionnaireData)
    {
        //var QData = GetObjectFromStream<FatcaQuestionnaire>fataQuestionnaireData);
        //FatcaQuestionnaire.Deserialize(fataQuestionnaireData);

        XDocument doc = XDocument.Parse(fatcaQuestionnaire.ToString(CultureInfo.InvariantCulture));

        // w.WriteLine("The value of doc" + doc);

        doc.Descendants("QuestionAnswer").ToList().ForEach(questionAnswer =>
        {
            var fataQuestionsAnswers = new QuestionAnswers();
            {
                //var questionAnswer = qa.Element("QuestionAnswer");

                var questionElement = questionAnswer.Element("Question");


                if (questionElement != null )

                    fataQuestionsAnswers.Question = questionElement.Value;

                //if (questionElement != null)
                //    w.WriteLine("The value of questionElement" + questionElement.Value);

                var answerElement = questionAnswer.Element("Answer");
                if (answerElement != null)
                    fataQuestionsAnswers.Answer = answerElement.Value;

                //if (answerElement != null)
                //    w.WriteLine("The value of answerElement" + answerElement.Value);

                var sqa = questionAnswer.Element("SubQuestionAnswer");
                if (sqa != null)

                {
                    var subQuestionElement = sqa.Element("Question");
                    if (subQuestionElement != null)
                        fataQuestionsAnswers.SubQuestionAnswer.Question = subQuestionElement.Value;

                    //if (subQuestionElement != null)
                    //    w.WriteLine("The value of answerElement" + subQuestionElement.Value);


                    var subAnswerElement = sqa.Element("Answer");
                    if (subAnswerElement != null)
                        fataQuestionsAnswers.SubQuestionAnswer.Answer = subAnswerElement.Value;

                    //if (subQuestionElement != null)
                    //    w.WriteLine("The value of answerElement" + subQuestionElement.Value);
                }

                if (questionnaire.QuestionAnswers.Find(a => a.Question != fataQuestionsAnswers.Question) == null)
                questionnaire.QuestionAnswers.Add(fataQuestionsAnswers);
                //fatcaQuestionsList.Add(fataQuestionsAnswers);
            }

            fatca.Questionnaire.Add(fataQuestionsAnswers);
        });
    }
}

改用Any

if(!questionnaire.QuestionAnswers.Any(a => a.Question == fataQuestionsAnswers.Question)) 
{ 
    questionnaire.QuestionAnswers.Add(fataQuestionsAnswers); 
}

可能存在尾随空格/大小写差异吗?

if(!questionnaire.QuestionAnswers.Any(a => a.Question.Trim().Equals(fataQuestionsAnswers.Question.Trim(), StringComparison.OrdinalIgnoreCase))) 
{ 
    questionnaire.QuestionAnswers.Add(fataQuestionsAnswers); 
}

你有你的条件不对,你正在寻找questionanswers其中question不匹配,你应该寻找在那里它们匹配和检查的结果为空。 (用==切换!=)

它应该是

if (questionnaire.QuestionAnswers.Find(a => a.Question == fataQuestionsAnswers.Question) == null)

但是,我将其更改为Any()因为它更接近且更易于阅读,如果列表中的一项与您指定的条件匹配,它将返回true。

if(!questionnaire.QuestionAnswers.Any(a => a.Question == fataQuestionAnswers.Question)) {
   questionnaire.QuestionAnswers.Add(fataQuestionsAnswers);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM