Query n:m using the Entity Framework

Asked

Viewed 41 times

0

My problem with doing a "Many-to-Many" query. Receiving a View ID. In other words, I receive an id from the selected framework. 1 Catechesis has a list of parishes and 1 Parochia has a list of Catecheses; A List of parishes has "1" trickery.
I receive the ID of the scam to select all parochials, this in the controller Catechesis. I hope to be explicit.

Method:

 public JsonResult GetParoquiasByVigararias(int id)
        {


            var paroquias = from p in db.Paroquia
                            from c in db.Catequese.Where(?????)
                            select new { p.ParoquiaID, p.Nome };


            return Json(paroquias, JsonRequestBehavior.AllowGet);
        }

Model Catequese:

  public partial class Catequese {

        public Catequese()
        {
            this.Paroquias = new HashSet<Paroquia>();
            this.Pessoas = new HashSet<Pessoa>();
            this.Inscricoes = new HashSet<Inscricao>();
        }

        //chave primária e forasteira
        [Key]
        public int CatequeseID { get; set; }
        public String NomeCatequese { get; set; }

        public virtual ICollection<Paroquia> Paroquias { get; set; }
        public virtual ICollection<Pessoa> Pessoas { get; set; }
        public virtual ICollection<Inscricao> Inscricoes { get; set; }
        //  public virtual ICollection<ParoquiaCatequese> Paroquia { get; set; }
    }

Model Paroquia:

  public partial class Paroquia {

        public Paroquia()
        {
            this.Catequese = new HashSet<Catequese>();
        }

        //chave primaria
        public int ParoquiaID { get; set; }

        [Display(Name = "Nome da Paróquia")]
        public String Nome { get; set; }

        //define a ligação a tabela vigararia
        [Display(Name = "Vigararia")]
        public int VigarariaID { get; set; }

        [ForeignKey("VigarariaID")]
        public virtual Vigararia Vigararia { get; set; }

        public virtual ICollection<Catequese> Catequese { get; set; }

    }

1 answer

0

Okay. I was complicating a no complication.

 public JsonResult GetParoquiasByVigararias(int id)
        {
            var paroquias = db.Paroquia
                .Where(p => p.VigarariaID == id)
                .Select(p => new { p.ParoquiaID, p.Nome })
              .OrderBy(p => p.Nome);  

            return Json(paroquias, JsonRequestBehavior.AllowGet);
        }

Browser other questions tagged

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