Junction of 2 Linq Queries

Asked

Viewed 124 times

2

I am using the mysql database. I have 2 tables one of questions and options related to the question. For every access I make to the system I need to take the questions randomly and obviously the related options. I found in google a way to take the questions randomly because mysql does not have the Guid as in Sql server. Below the code:

var teste = (from q in db.Questoes orderby rnd.Next() select q).Take(2)

Now how do I Join this query with my options table? Join is done with the field Questoes.Id and Opcoes.QuestoesId

public class QuestaoModel
{
    public int Id { get; set; }
    [Column(TypeName = "varchar(MAX)")]
    public string Pergunta { get; set; }
    public int TemaId { get; set; }
    public virtual TemaModel Tema { get; set; }
    public virtual ICollection<OpcaoModel> Opcoes { get; set; }
    public virtual RespostaModel Resposta { get; set; }
}

public class OpcaoModel
{
    public int Id { get; set; }
    public string opcaoResposta { get; set; }
    public int QuestaoId  { get; set; }
    public virtual QuestaoModel Questao { get; set; }
}

Can someone give me a hand?

  • It’s not just giving one include of the relationship??

2 answers

2

Try to use something like db.Questoes.Include(i => i.Opcoes) to bring the options in the result.

  • OP is using another style in his LINQ.

  • I believe that the result would be the same because it is possible to merge the two styles of consultation.

  • 1

    Yes, you are absolutely right, I only mentioned this because it is good practice to adopt a style, mixing several can be confusing for reading.

  • 1

    I agree with you.

1


From what you described I think that’s it:

var teste = 
    (from q in db.Questoes 
    join op in db.Opcoes on q.Id equals op.QuestoesId 
    orderby rnd.Next() select q)
    .Take(2)
  • Mauro, your solution worked, but there’s only one detail. In my select one use a template which is the following: public class Questaoopcaomodel { public int Questaoid { get; set; } public string Questaopergunta { get; set; } public int Opcaoid { get; set; } public string Optiondescription { get; set; } } And when I do this load the 2 questions but with only 1 option each. And cadas question has 5 options. How do I not lose them?

  • If it worked accepted the answer Marcelo, anything that was not as you want to say and I edit the answer

  • <br/> The problea I’m having is in select because I carry a model. below is it: <br/> language:c# public class Questaoopcaomodel { public int Questaoid { get; set; } public string Questaopergunta { get; set; } public int Opcaoid { get; set; } public string Optiondescription { get; set; } } <br/> But when doing this appears the 2 questions but with only 1 option each and in fact are 5 options each question. How can you solve this problem?

  • But that’s another question, you have to ask another question, it’s not part of the same.

  • okay. I’ll do it then

  • confirm as correct the answer I gave Marcelo

Show 1 more comment

Browser other questions tagged

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