1

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.

1
  • Do you have any exception? Commented Dec 21, 2015 at 13:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

item will never be null because you have used Single and not SingleOrDefault, therefore if it doesn't find it then it will throw an exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.