简体   繁体   English

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

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

The code below contains a foreach loop that loops through a list of string collection which contains XML. 下面的代码包含一个foreach循环,该循环遍历包含XML的字符串集合列表。 While enumerating through the collection, it reads the question and answer elements and adds them to the list collection. 在枚举集合时,它将读取问题和答案元素并将其添加到列表集合。 I need to ensure that no repeated question is added to the list collection. 我需要确保没有重复的问题添加到列表集合。

The code below questionnaire.QuestionAnswers.Add(fataQuestionsAnswers) adds the elements to the list collection. questionnaire.QuestionAnswers.Add(fataQuestionsAnswers)下面的代码questionnaire.QuestionAnswers.Add(fataQuestionsAnswers)将元素添加到列表集合。 The problem that I am facing is that the repeated questions are getting added to the list. 我面临的问题是重复的问题已添加到列表中。 I tried to put the following condition that is: 我试图提出以下条件:

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

but that doesn't seem to work. 但这似乎不起作用。

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

Use Any instead 改用Any

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

Is it possible there's trailing spaces / casing differences? 可能存在尾随空格/大小写差异吗?

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

You have your condition wrong, you are looking for questionanswers where the question does not match, you should be looking where they do match and checking that the result is null. 你有你的条件不对,你正在寻找questionanswers其中question不匹配,你应该寻找在那里它们匹配和检查的结果为空。 (switch the != with ==) (用==切换!=)

It should be 它应该是

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

However I would change it to Any() as it is a bit nearer and easier to read, it returns a true if one of the items in your list matches the condition that you specify. 但是,我将其更改为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