user5685616
user5685616

Reputation:

How to update an existing item in SQL database using linq?

i would like to update an existing item in a sql database using linq.
i have tried with this.

    public void UpdateFoodDrinkTobacco(int id, string preservationTechniqueCodes, bool isHomogenised)
        {
            var item = (from item in _db.FoodDrinkAndTobacco 
                        where item.id equals id 
                        select item.isHomogenised);
            item = isHomogenised;


            _db.SubmitChanges();
        }

but this is not working for me.
Hope some of you have a suggestion on how to do this.

Upvotes: 1

Views: 89

Answers (1)

Code_Viking
Code_Viking

Reputation: 628

You could do somthing like this.

public void UpdateFoodDrinkTobacco(int id, string preservationTechniqueCodes, bool isHomogenised)
    {
        var item = _db.FoodDrinkTobaccos.Where(i => i.id == id).Select(i => i);
        if (item == null) return;

        item.preservationTechniqueCodes = preservationTechniqueCodes;
        item.isHomogenised = isHomogenised;

        _db.SubmitChanges();
    }

like this you can change all of the parameters. And i think i looks a little better when you use lambda expressions instead of the other way you did it. Hope this works.

Upvotes: 1

Related Questions