在c#中查找列表中的项

时间:2015-06-22 21:11:42

标签: c# linq

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

以下代码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);
        });
    }
}

3 个答案:

答案 0 :(得分:0)

使用Any代替

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

答案 1 :(得分:0)

是否有可能出现尾随空格/套管差异?

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

答案 2 :(得分:0)

您的条件有误,您正在寻找questionanswers不匹配的question,您应该查看它们匹配的位置并检查结果是否为空。 (切换!= with ==)

应该是

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