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>

सभी प्रश्न प्राप्त करें

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
        }
    }
}
0
shiv mysuru 5 अगस्त 2019, 13:40

1 उत्तर

सबसे बढ़िया उत्तर

निम्नलिखित का प्रयास करें:

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();
0
Ekevoo 5 अगस्त 2019, 23:34