JSON
下面是存储过程的输出。我想生成一个根据问题类型的问题分为几类的类,并且每个问题都有答案作为列表。
我的班级层次结构:
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
},
我将其除以
var result = result.GroupBy(u => u.QuestionTypeId).Select(grp =>
grp.ToList()).ToList();
但是我无法将其添加到列表中。
我想将结果映射到上面的类层次结构中。
答案 0 :(得分:1)
假设您有类似的东西
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; }
}
您可以使用:
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();
答案 1 :(得分:0)
现在您正在将类QuestionTypeList和QuestionList声明为通用列表,例如:
public List<QuestionList> QuestionList{ get; set; }
一个选择可能是更改两个类以实现列表,使用这种方法,您将能够覆盖列表方法(如“ add”和“ exists”),将所有添加新逻辑所需的逻辑放入内部列表中的项目。