不能隐式将类型转换为 System.Collections.Generic.List

本文关键字:System Collections Generic List 类型转换 不能 | 更新日期: 2023-09-27 18:32:32

我正在尝试硬编码一些数据进行测试,但似乎无法使其正常工作。 我确定我错过了一些简单的东西。

这是我的代码:

public async Task<ActionResult> GetClusterAnswers(int clusterId, int sectionId)
{
    contractorId = UserInfo.targetCompanyID;
    var questions = await CommonClient.GetGeneralQandAsBySection(sectionId, contractorId);
    var selectedQuestion = questions.FirstOrDefault(q => q.QuestionClusterID == clusterId);
    int? questionid = selectedQuestion.QuestionID;
    QuestionsWithPairedAnswers question = new QuestionsWithPairedAnswers();
    question.QuestionID = questionid;
    question.vchQuestionText = selectedQuestion.vchQuestionText;
    question.vchTextElementOneHeader = selectedQuestion.vchTextElementOneHeader;
    question.vchTextElementTwoHeader = selectedQuestion.vchTextElementTwoHeader;
    question.Answers = new PairedAnswerTypes()
    {
        QuestionID = question.QuestionID,
        PairedTextElementAnswerID = 1,
        ContractorID = contractorId,
        vchTextElementOne = "ABC",
        vchTextElementTwo = "School Teachers"
    };
    return Json(question, JsonRequestBehavior.AllowGet);
}

这是我的模型:

public class QuestionsWithPairedAnswers
{
    [Key]
    public int? QuestionID { get; set; }
    public string vchQuestionText { get; set; }
    public string vchTextElementOneHeader { get; set; }
    public string vchTextElementTwoHeader { get; set; }
    public List<PairedAnswerTypes> Answers { get; set; }
}
public class PairedAnswerTypes
{
    public int PairedTextElementAnswerID { get; set; }
    public int? QuestionID { get; set; }
    public int ContractorID { get; set; }
    public string vchTextElementOne { get; set; }
    public string vchTextElementTwo { get; set; }
    public virtual QuestionsWithPairedAnswers Question { get; set; }
}

任何帮助将不胜感激!

不能隐式将类型转换为 System.Collections.Generic.List

问题出在这一行:

question.Answers = new PairedAnswerTypes()
        {
            QuestionID = question.QuestionID,
            PairedTextElementAnswerID = 1,
            ContractorID = contractorId,
            vchTextElementOne = "ABC",
            vchTextElementTwo = "School Teachers"
        };

问题。答案是PairedAnswerTypesList,您为其分配单个PairedAnswerTypes,您可以将此赋值更改为列表初始化和赋值:

question.Answers = new List<PairedAnswerTypes> {
    new PairedAnswerTypes()
        {
            QuestionID = question.QuestionID,
            PairedTextElementAnswerID = 1,
            ContractorID = contractorId,
            vchTextElementOne = "ABC",
            vchTextElementTwo = "School Teachers"
        }
};

QuestionsWithPairedAnswers 中的Answers属性是List<PairedAnswerTypes>,但在以下行中:

question.Answers = new PairedAnswerTypes()

您尝试将其设置为 PairedAnswerTypes

将其更改为:

question.Answers = new List<PairedAnswerTypes>
{
    new PairedAnswerTypes()
    {
        QuestionID = question.QuestionID,
        PairedTextElementAnswerID = 1,
        ContractorID = contractorId,
        vchTextElementOne = "ABC",
        vchTextElementTwo = "School Teachers"
    }
}

这样,您就可以根据属性的要求将新答案放入新List中。

相关文章: