How to convert query from sql to linq?

3

I need to convert the following query to linq

select * from detalle_pedido
where articuloid = 13555 and id_pedido=13

I've tried it in the following way

var querydetalle = _db.detalle_pedido.Where(dt => dt.articuloid == articu.articuloid || ped.id_pedido == idpedido);
var dtp = querydetalle.FirstOrDefault();

but I can not get the value I should get. What should I modify?

    
asked by Andrex_11 30.10.2018 в 16:36
source

3 answers

2

You have two errors:

  • The operator for AND is && no ||

  • The variable to which you must compare the request is dt no ped

  • It should look like this:

    var querydetalle = _db.detalle_pedido.Where(dt =>
        dt.articuloid == articu.articuloid &&
        dt.id_pedido == ped.idpedido);
    var dtp = querydetalle.FirstOrDefault();
    
        
    answered by 30.10.2018 / 16:51
    source
    1

    In your query, the operator you are using is OR, you should use & amp; & to work for you and then list the values in a list, your code would be like this,

      var querydetalle = _db.detalle_pedido.Where(dt => dt.articuloid == articu.articuloid && det.id_pedido == ped.idpedido).ToList();
    
    return querydetalle; 
    
        
    answered by 30.10.2018 в 16:43
    -1

    The operator you are using || is OR in expressions LAMBDA the operator for AND is & & Your query would be like this:

      var querydetalle = _db.detalle_pedido.Where(dt => dt.articuloid == articu.articuloid && ped.id_pedido == idpedido);
      var dtp = querydetalle.FirstOrDefault(); 
    
        
    answered by 30.10.2018 в 16:40