Associate a set of tables using linq

0

Hello, I am trying to reference a set of tables to be able to use the data of some records. For this he used the following:

    void asociarcamposforms() {
                var idpedido = Convert.ToDecimal(Request.QueryString["idpedido"]);

                var queryObtenerdtpedido = db.detalle_pedido.Where(dt => dt.id_pedido == idpedido);
                var pedido = queryObtenerdtpedido.FirstOrDefault();

                var idarticulo = pedido.articuloid;

                var queryObtenerArticulo = db.articuloes.Where(p => p.articuloid == idarticulo);
                var articulos = queryObtenerArticulo.FirstOrDefault();
}

works perfectly, but I would like to know if there is any way to reduce all that procedure I do. This is just a fragment of code the other processes are conditions and methods

    
asked by Andrex_11 25.09.2018 в 22:02
source

1 answer

3

By joining with the tables, so only one query is made instead of two.

void asociarcamposforms()
{
    var idpedido = Convert.ToDecimal(Request.QueryString["idpedido"]);

    var query = from p in db.detalle_pedido
                join a in db.articuloes on p.articuloid equals a.articuloid
                where p.id_pedido == idpedido
                select a;

    var articulos = query.FirstOrDefault();
}
    
answered by 25.09.2018 / 22:21
source