1
What would this query look like in LINQ to Entities:
SELECT COUNT(cont) qtde_resp
, pergunta
, resposta
FROM pesq_respostas
WHERE id_pesquisa = 9
AND Tipo IN ('Intervalo', 'SimNao')
GROUP BY pergunta
, resposta
I tried to do it this way:
dynamic countTag = (from a in context.PesqRespostaModel
where a.id_pesquisa == idPesquisa
group new { a } by new { a.resposta, a.pergunta } into gr
select new
{
gr.Key.pergunta,
gr.Key.resposta,
Count = gr.Select(x => x.a.cpf).Distinct().Count()
}).ToList();
However, it makes the following mistake:
but this field exists, both in the bank and in the class.
Follows my model:
[Table("pesq_respostas")]
public class PesqRespostaModel
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id_resposta { get; set; }
[Required]
public int id_pesquisa{ get; set; }
[Required]
[Display(Name = "pergunta")]
[StringLength(128, ErrorMessage = "O campo Resposta pode ter mo máximo 128 caracteres.")]
public string pergunta { get; set; }
[Required]
[Display(Name = "resposta")]
[StringLength(48, ErrorMessage = "O campo Resposta pode ter mo máximo 48 caracteres.")]
public string resposta { get; set; }
public string tipo { get; set; }
[Required]
[Display(Name = "cpf")]
[StringLength(14, ErrorMessage = "O campo Resposta pode ter mo máximo 14 caracteres.")]
public string cpf { get; set; }
[Required]
[Display(Name = "data")]
public DateTime data { get; set; }
}
Instead of having
group new { a } by new { a.resposta, a.pergunta } into gr
just putgroup a by new { a.resposta, a.pergunta } into gr
.– João Martins
John, I did as you said, but it was the same mistake.
– alessandre martins
You can put your model in your question?
– João Martins
updated, placing the model
– alessandre martins
Where the object comes from
Project2
which is indicated in the error? And the propertyresposta
in the clausewhere
if it’s not there?– João Martins
João, I don’t know where this project2 comes from, I believe that the framework creates this, strange. It has to do with Count, when I remove Count, everything works.
– alessandre martins
Change the
Count()
to the following:Count = gr.Count()
and validate if it already works.– João Martins