I cannot figure out how to construct a lambda to find a node when the hierarchy is ragged. I'm debating changing the object model to not be ragged or ask if someone can help with the lambda in the case of the same below.
Given I have a survey object, which has a list of questions objects and each question has list of response objects, the issue is a response can lead to another question. I didn't know how to construct the lambda to search not only all the questions in the survey, but also all the responses which may also have questions, and on down to the end of all possible paths. What I haven't tried is to basically give up and reconfigure the object model so I just have a flat list of questions and responses.
public void OnPostAddResponse(int questionid)
{
//find question so we can add response to it
SurveyQuestion q = this.SurveyObject.Questions.First(x => x.QuestionId == questionid);
if (q.ResponseList == null)
{
q.ResponseList = new List<SurveyResponse>();
}
int newid = (q.QuestionId * 1000) + q.ResponseList.Count + 1;
q.ResponseList.Add(new SurveyResponse(newid));
}
public class Survey
{
public string SurveyName { get; set; }
public List<SurveyQuestion> Questions { get; set; }
public Survey()
{
this.SurveyName = "new survey name here";
this.Questions = new List<SurveyQuestion>();
}
}
public class SurveyQuestion
{
public int QuestionId { get; set; }
public string Question { get; set; }
public List<SurveyResponse> ResponseList { get; set; }
public SurveyQuestion() { }
public SurveyQuestion(int id)
{
this.QuestionId = id;
this.Question = "question text for id " + id;
this.ResponseList = new List<SurveyResponse>();
}
}
public class SurveyResponse
{
public int ResponseId { get; set; }
public string Response { get; set; }
public SurveyQuestion NextQuestion { get; set; }
public SurveyResponse() { }
public SurveyResponse(int id)
{
this.ResponseId = id;
this.Response = "response text for id " + id;
}
}
What I'd like is for the OnPostAddResponse to be able to search the entire survey object and find the question ID passed in even though it could be a question that is under a response object.
Or, should instead reconfigure the object model so that a survey has a flat list of questions and responses which are then connected by their ID fields. I think this would solve the issue but I am not sure if it would make other aspects more difficult down the road.