0

I want to remove an item from list, I searched, I tried all versions of Remove(Remove, RemoveAt, RemoveAll) methods, but I couldn't figure it out. The item is always there. My Code :

foreach (Question q in questions)
{
    foreach (QuestionValue qv in q.QuestionValueList)
    {
        if (m_User.OrganizationRole.QuestionValuePermissionList.Where(p => p.QuestionValue.QuestionValue_Id == qv.QuestionValue_Id).Count() == 0)
        {
            int i = questions.FindIndex(p => p.Question_Id == q.Question_Id);
            QuestionValue item = questions[i].QuestionValueList.Find(p => p.QuestionValue_Id == qv.QuestionValue_Id);

            questions[i].QuestionValueList.Remove(item);
        }
    }
}
  • Always include full error messages. Which when read, should answer your question. – H H Jan 20 '18 at 07:25
  • Actually there is no error, it runs without an error, but it doesn't remove the item. – Deniz Kılıç Jan 20 '18 at 07:27
  • 1
    The code doesn’t make sense. You already have the `Question` object so why look for it again? Also note you can’t iterate a collection and modify it at the same time. That will cause an issue. But first, have you run the code in a debugger line by line and see what it does exactly? – Sami Kuhmonen Jan 20 '18 at 07:28
  • foreach (Question q in questions) { foreach (QuestionValue qv in q.QuestionValueList) { if (m_User.OrganizationRole.QuestionValuePermissionList.Where(p => p.QuestionValue.QuestionValue_Id == qv.QuestionValue_Id).Count() == 0) { q.QuestionValueList.Remove(qv); } else { } } } – Deniz Kılıç Jan 20 '18 at 07:45
  • Do you think anyone wants to read that? If you have something new, _edit_ the question. – H H Jan 20 '18 at 16:37

0 Answers0