Doubt with LINQ to Entities

Asked

Viewed 70 times

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:

erro

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 put group a by new { a.resposta, a.pergunta } into gr.

  • John, I did as you said, but it was the same mistake.

  • You can put your model in your question?

  • updated, placing the model

  • Where the object comes from Project2 which is indicated in the error? And the property resposta in the clause where if it’s not there?

  • 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.

  • Change the Count() to the following: Count = gr.Count() and validate if it already works.

Show 2 more comments
No answers

Browser other questions tagged

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