Select all items with the same ID in the same table

1

Very well, I'll try to be as clear as possible. I have the stock table where you will have several products there - > ProductId and its Quantity. In the same table the productId can be repeated with a different quantity, in my current code I can only get the first quantity in the table but I can not get the other one and add the two. use entity framework using lambda.

            Estoque estoque = db.Estoque.Where(x => x.ProdutoId == model.ProdutoId).FirstOrDefault();
        int qtdEstoque = estoque.Quantidade;

Returns = 10 being that it had to be Quantity = 30

    
asked by anonymous 17.02.2018 / 00:52

1 answer

2

You will not be able to add the two quantities in this way, the best way to solve this would be, instead of using FirstOrDefault() , use ToList() to query, thus creating a Stock List of the same Produto Id . Then work with this list, make a ForEach in it and add the Quantities. Something like this:

var estoques = db.Estoque.Where(x => x.ProdutoId == model.ProdutoId).ToList();

int quantidadeEstoque = 0;

foreach(var estoque in estoques){
            quantidadeEstoque += estoque.Quantidade;
};

This way you can get all stocks with less than Produto_Id .

    
17.02.2018 / 03:44