Reputation: 159
Below JSON
is the output from a stored procedure. I would like to generate a class which divides into group of classes based on question type questions and it has answers as a list to each question.
My class hierarchy :
public class QuestionarieDisplay
{
public QuestionarieDisplay()
{
QuestionTypeList = new List<QuestionTypeList>();
}
public Nullable<int> QuestionnaireId { get; set; }
public Nullable<int> QuestionnaireGroupId { get; set; }
public string QuestionnaireGroup { get; set; }
public List<QuestionTypeList> QuestionTypeList { get; set; }
}
public class QuestionTypeList
{
public QuestionTypeList()
{
QuestionList= new List<QuestionList>();
}
public Nullable<int> QuestionTypeId { get; set; }
public string QuestionType { get; set; }
public List<QuestionList> QuestionList{ get; set; }
}
public class QuestionList
{
public QuestionList()
{
Answer= new List<Answer>();
}
public List<Answer> Answer { get; set; }
public Nullable<int> QuestionId { get; set; }
public string Question { get; set; }
public Nullable<int> ScaleId { get; set; }
public string ScaleType { get; set; }
public string Attribute { get; set; }
public string AttributeValue { get; set; }
}
public class Answer
{
public string Answers { get; set; }
}
{
"QuestionnaireId": 2,
"QuestionnaireGroupId": 1,
"QuestionnaireGroup": "Please rank based on the following areas",
"QuestionId": 2,
"Question": "Full Name?",
"QuestionTypeId": 3,
"QuestionType": "Short Text",
"ScaleId": null,
"ScaleType": null,
"Answers": null,
"Attribute": "Required",
"AttributeValue": "Yes"
},
{
"QuestionnaireId": 2,
"QuestionnaireGroupId": 1,
"QuestionnaireGroup": "Please rank based on the following areas",
"QuestionId": 5,
"Question": "Experience?",
"QuestionTypeId": 9,
"QuestionType": "Dropdown",
"ScaleId": null,
"ScaleType": null,
"Answers": null,
"Attribute": null,
"AttributeValue": null
},
{
"QuestionnaireId": 2,
"QuestionnaireGroupId": 1,
"QuestionnaireGroup": "Please rank based on the following areas",
"QuestionId": 7,
"Question": "Email Adddress?",
"QuestionTypeId": 3,
"QuestionType": "Short Text",
"ScaleId": null,
"ScaleType": null,
"Answers": null,
"Attribute": null,
"AttributeValue": null
},
{
"QuestionnaireId": 2,
"QuestionnaireGroupId": 1,
"QuestionnaireGroup": "Please rank based on the following areas",
"QuestionId": 12,
"Question": "Your comments?",
"QuestionTypeId": 4,
"QuestionType": "Long Text",
"ScaleId": null,
"ScaleType": null,
"Answers": null,
"Attribute": null,
"AttributeValue": null
},
I have divided it by:
var result = result.GroupBy(u => u.QuestionTypeId).Select(grp =>
grp.ToList()).ToList();
but I am not able to add it to list.
I would like to map result in the above class hierarchy.
Upvotes: 3
Views: 119
Reputation: 948
Assuming that you have something like
public class RawDataDTO
{
public Nullable<int> QuestionnaireId { get; set; }
public Nullable<int> QuestionnaireGroupId { get; set; }
public string QuestionnaireGroup { get; set; }
public Nullable<int> QuestionTypeId { get; set; }
public string QuestionType { get; set; }
public List<Answer> Answer { get; set; }
public Nullable<int> QuestionId { get; set; }
public string Question { get; set; }
public Nullable<int> ScaleId { get; set; }
public string ScaleType { get; set; }
public string Attribute { get; set; }
public string AttributeValue { get; set; }
}
you could use:
List<RawDataDTO> raw = // your data in raw format
List<QuestionarieDisplay> list = raw
.GroupBy(r => new { r.QuestionnaireGroup, r.QuestionnaireGroupId, r.QuestionnaireId })
.Select(r => new QuestionarieDisplay()
{
QuestionnaireId = r.Key.QuestionnaireId,
QuestionnaireGroupId = r.Key.QuestionnaireGroupId,
QuestionnaireGroup = r.Key.QuestionnaireGroup,
QuestionTypeList =
r.GroupBy(t => new { t.QuestionType, t.QuestionTypeId })
.Select(t => new QuestionTypeList
{
QuestionType = t.Key.QuestionType,
QuestionTypeId = t.Key.QuestionTypeId,
QuestionList = t.Select(x => new QuestionList
{
QuestionId = x.QuestionId
})
.ToList()
})
.ToList()
}).ToList();
Upvotes: 1
Reputation: 1700
Right now your are declaring the classes QuestionTypeList and QuestionList as generic lists like in:
public List<QuestionList> QuestionList{ get; set; }
An option could be to change both classes to implement a list, using this approach, you are going to be able to override the list methods like 'add' and 'exists' putting inside all the logic you need to add a new item to the list.
Upvotes: 0