Query n: m using the Entity Framework

0

My problem with doing a "many-to-many" query. Receiving a View ID. In other words, I get an id from the selected cheater.  1 Catechesis has a list of parishes and 1 Parish has a list of Catecheses; A List of parishes has "1" truncheon.
I get the trinket ID to select all the parishes, this in the Catequese controller. 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 Catechesis:

  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 Parish:

  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; }

    }
    
asked by anonymous 20.01.2016 / 17:03

1 answer

0

Okay. was complicating one there was 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);
        }
    
20.01.2016 / 17:32