Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to get the database value after being assigned from another method

$
0
0

I am doing a foreach loop to get the values from the dabase of a model or table,

then in another method I want to get again the values from the database, but what happens that the values i get are from the first method where they were assigned

to make this undertandable here is an example hope it gets understood

In this example in the foreach loop I want to get the Price from the dabase value and add 100, so if the dabase value is 50 then the pord.price would be 150

But then in the GetPrice method I want to get the Price from the database and add 50 so that the Price would be 100, But it does not, it is 200

What happens it gets the 150 from the foreach loop, and the 50 that I addes in the GetPrice() Method

My Question would be, how to get the dabase value from the GetPrice() Method, instead of the already assigned in the foreach loop

I thought that each method had its own scope

        public IActionResult Index()
        {
            foreach(var product in db.Products)
            {
                product.price = product.price + 100; //it was 50 so now it is 150, which is what I want
//and do something with product.price } GetPrice(); return View(); } private void GetPrice() { var product = db.Products.Single(P => P.productId == 1); var price = product.price + 50;// I expected 50 + 50 = 100, but it gets 150 + 50 = 200 which I don't want
//and do somethingelse with price }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>