Query Linq C #

0

I have a small problem and I find a solution.

I have to take the name of a person and with the name of the school

    DB_Entities db = new DB_Entities();   // es generada por el Entitie framework
    public JsonResult GetPersona(string codPersona)
    {
        int id = Convert.ToInt32(codPersona);

        var persona = (from pe in db.Persona
                       join co in db.Colegio on pe.CodColegio equals co.IdColegio
                       where pe.CodPersona == id
                       select new
                       {
                           NombreCompleto = pe.NombreCompleto,
                           Colegio = co.Nombre
                       }).FirstOrDefault();

        return new JsonResult { Data = persona, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

    
asked by Rodrigo Rodriguez 25.10.2018 в 18:36
source

1 answer

0

I like more the landa expressions for LINQ queries, I hope you find it useful.

int id = Convert.ToInt32(codPersona);

var persona = db.Persona.Include(p => p.Colegio).FirstOrDefault(p => p.CodPersona == id);

then you can access persona.Colegio.Nombre in case you need the name of the school.

    
answered by 25.10.2018 в 20:29