How to make a SELECT with Entity Framework?

Asked

Viewed 7,457 times

2

I’m developing a kind of a C#QUIZ, and I’ve managed to do the INSERT part of the questions and answers in the bank, but I’m having difficulty bringing the question and the answers from the bank to the quiz.

Kind of a select, because I just found select with Entity in list form, and I need to make a select that it brings the question and the answers.

  • 3

    Like this your model?

  • Guy created in a table Game that contains the fields Question, Answerone, Answertwo, Answerright and Answercertain...

  • Then I installed the Entity and even managed to insert in the bank with an Entity method, only I could not find any material to select.

  • Edit your question and post your model.

1 answer

2


Are these questions in the same entity as the answers? If it is you need to use an anonymous type:

var Resultado = Jogo.Select(x => new {x.Pergunta, x.RespostaUm, x.RespostaDois, x.RespostaTres, x.RespostaCerta});

If they are in separate tables, you have to join with LINQ:

var Resultado = from Pergunta in Jogo
join Resposta in Respostas 
on Pergunta equals Respostas.Pergunta
select new { TextoPergunta = Jogo.Pergunta, TextResposta = Resposta.RespostaUm };

It is always worth noting that all LINQ is based on the SQL language, so it is not so difficult to understand the commands as long as you have basic knowledge of SQL.

References: https://msdn.microsoft.com/pt-br/library/bb397696.aspx

  • Valew, it worked great here.

Browser other questions tagged

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