我有以下两个类:
public class TestQuestionModel
{
public TestQuestionModel()
{
this.Answers = new List<TestQuestionAnswerModel>();
}
public int TestId { get; set; }
public int TestQuestionId { get; set; }
public int QuestionNumber { get; set; }
public virtual ICollection<TestQuestionAnswerModel> Answers { get; set; }
}
public class TestQuestionAnswerModel
{
public int AnswerUId { get; set; }
public bool? Correct { get; set; }
public bool? Response { get; set; }
public string Text { get; set; }
}
我想要做的是将Answers存储到一个字符串中并放入这个类:
public class TestQuestion
{
public int TestId { get; set; }
public int TestQuestionId { get; set; }
public int QuestionNumber { get; set; }
public string AnswerString { get; set; }
}
有人可以告诉我如何在两个方向上做到这一点(把它放在字符串中并将其取出)
答案 0 :(得分:0)
可能的方式:
public string Serialize(ICollection<TestQuestionAnswerModel> answers)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(answers);
}
public ICollection<TestQuestionAnswerModel> Deserialize(string data)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return (ICollection<TestQuestionAnswerModel>)serializer.Deserialize<ICollection<TestQuestionAnswerModel>>(data);
}