无法将类型'System.Collections.Generic.List <System.Linq.IGrouping <int,XXX >>'隐式转换为'System.Collections.Generic.List <XXX >>

时间:2019-08-05 10:40:00

标签: c# asp.net-mvc entity-framework linq

DB design of the tables

我正在尝试以List<QuestionsAndChoices>方法返回getAllQuestions(),但出现此错误

Cannot implicitly convert type 'System.Collections.Generic.List<System.Linq.IGrouping<int, HowWellDoYouKnowMeAPI.Models.models.QuestionsAndChoices>>' to 'System.Collections.Generic.List<HowWellDoYouKnowMeAPI.Models.models.QuestionsAndChoices>

GetAllQuestions

public List<QuestionsAndChoices> getAllQuestions()
{
    List<QuestionsAndChoices> questionsAndChoices1 = context.quizQuestionAndChoices.
        Join(context.quizQuestions, QandC => QandC.QuizQuestions.QuizQuestionsId, qustions => qustions.QuizQuestionsId, (QandC, qustions) =>
            new
            {
                QandC,
                qustions
            }).
            Join(context.quizChoices, QandC1 => QandC1.QandC.QuizChoicesId, choices => choices.QuizChoicesId, (QandC1, choices) =>
            new QuestionsAndChoices
            {
                QuestionId = QandC1.qustions.QuizQuestionsId,
                Question = QandC1.qustions.Questions,
                Choices = choices,
             }).GroupBy(e => e.QuestionId).ToList();

    return questionsAndChoices1;
}

QuestionsAndChoices

public class QuestionsAndChoices
    {
        public int QuestionId { get; set; }
        public string Question { get; set; }
         public List<QuizChoices> Choices { get; set; }
    }

QuizQuestionAndChoices

public class QuizQuestionAndChoices
{
    public int QuizQuestionAndChoicesID { get; set; }
    public int QuizChoicesId { get; set; }
    public int QuizQuestionsId { get; set; }
    public QuizChoices quizChoices { get; set; }
    public QuizQuestions QuizQuestions { get; set; }
}

quizQuestions

 public class QuizQuestions
 {
     public int QuizQuestionsId { get; set; }
     public string Questions { get; set; }
     public List<QuizzerQCdetails> quizzerQCdetails { get; set; }
     public List<QuizQuestionAndChoices> QuizQuestionAndChoices { get; set; }
 }

QuizChoices

public class QuizChoices
{
    public int QuizChoicesId { get; set; }
    public string Choice { get; set; }
    public List<QuizzerQCdetails> quizzerQCdetails { get; set; }
    public List<QuizQuestionAndChoices> quizQuestionAndChoices { get; set; 
}

我希望这样的输出

{
    "questionId":1,
    "question":"q1",
    "choices":{
        {
        "quizChoicesId":1,
        "choice":"1c1",
        "quizzerQCdetails":null,
        "quizQuestionAndChoices":null
        },
        {
        "quizChoicesId":2,
        "choice":"1c2",
        "quizzerQCdetails":null,
        "quizQuestionAndChoices":null
        }
    }
}

但正在获取此输出 @IbraHimM.Nada我已经遍历了这段代码,但是得到了这样的输出

{
"questionId":1,
"question":"q1",
"choices":{
        {
        "quizChoicesId":1,
        "choice":"1c1",
        "quizzerQCdetails":null,
        "quizQuestionAndChoices":null
        }
    }
}
,
{
"questionId":1,
"question":"q1",
"choices":{
        {
        "quizChoicesId":2,
        "choice":"1c2",
        "quizzerQCdetails":null,
        "quizQuestionAndChoices":null
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

List<QuestionsAndChoices> questionsAndChoices1 = (
    from qandc in context.quizQuestionAndChoices
    join quest in context.quizQuestions on qandc.QuizQuestionsId equals quest.QuizQuestionsId
    join answers in context.quizChoices on qandc.QuizChoicesId equals answers.QuizChoicesId
    select new { question = quest.Questions, answers = answers })
  .GroupBy(x => x.question)
  .Select(x => new QuestionsAndChoices()
    {
      Question = x.Key,
      Choices = x.Select(y => y.answers).ToList()
    }).ToList();