Problems with select command of Line

Asked

Viewed 22 times

0

The query worked she has 2 questions randomly with the 5 options each. The problem is that my select loads the view described below:

 public class QuestaoOpcaoModel
    {
        public int QuestaoId { get; set; }
        public string QuestaoPergunta { get; set; }
        public int OpcaoId { get; set; }
        public string OpcaoDescricao { get; set; }
    }

Aí tentei o seguinte código:
```c#
(from q in _educ365Context.Questoes
                 join op in _educ365Context.Opcoes on q.Id equals op.QuestaoId
                 where q.TemaId == int.Parse(tema)
                 orderby rnd.Next()
                 select new QuestaoOpcaoModel
                 {
                     QuestaoId = q.Id,
                     QuestaoPergunta = q.Pergunta,
                     OpcaoId = op.Id,
                     OpcaoDescricao = op.opcaoResposta
                 })
                .Take(2);
E ao fazer isso me trouxe as 2 perguntas mas com apenas 1 opção sendo que e, cada pergunta existem 5 opções vinculas a ela. Como posso fazer para resolver isso?

1 answer

1

I managed to solve the problem, I only dismembered my query in two. The first to take the questions randomly:

var questions = (from q in db.Questoes where q.TemaId == int.Parse(tema) orderby rnd.Next() select q).Take(2);

The second makes a Join of the first with the table of related options:

prova = (from q in questions
                            join op in db.Opcoes on q.Id equals op.QuestaoId
                            select new QuestaoOpcaoModel
                            {
                                QuestaoId = q.Id,
                                QuestaoPergunta = q.Pergunta,
                                OpcaoId = op.Id,
                                OpcaoDescricao = op.opcaoResposta
                            }).AsQueryable();

And it worked. No options were lost on my model Questaoopcaomodel. But if you have any opinions to make it easier, do not hesitate to send

  • Good Marcelo, glad you made it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.