I've done a lot of reading on copying/cloning objects in C# and I was trying to work from this example, but my situation is a little different.
I have a list of question/answer pairs being returned from the database. I want to use that to create two lists for parent and child objects. When I do that, any changes I make to the child are reflected in the parent.
I've tried several permutations of creating new lists, cloning, etc, but always end up with the same result.
List<QuestionVM> questionlist = productRepository.GetQuestions();
parent.Questions = AddQuestions(true, parent, questionlist);
child.Questions = AddQuestions(false, child, questionlist);
private List<QuestionVM> AddQuestions(bool parent, Line line, List<QuestionVM> questions)
{
//shouldn't this create a new object, not just a reference?
List<QuestionVM> qs = new List<QuestionVM>(questions);
if (parent)
{
return qs.Where(w => w.ShowOnParent).ToList();
}
else
{
return qs.ToList();
}
}
Any help is greatly appreciated.