Update records using LINQ

后端 未结 8 1581
迷失自我
迷失自我 2020-12-04 17:24

I need to set a value in a table for a subset of rows. In SQL, I would do this:

UPDATE dbo.Person SET is_default = 0 WHERE person_id = 5

Is

8条回答
  •  天命终不由人
    2020-12-04 17:53

    Just as an addition to the accepted answer, you might find your code looking more consistent when using the LINQ method syntax:

    Context.person_account_portfolio
    .Where(p => person_id == personId)
    .ToList()
    .ForEach(x => x.is_default = false);
    

    .ToList() is neccessary because .ForEach() is defined only on List, not on IEnumerable. Just be aware .ToList() is going to execute the query and load ALL matching rows from database before executing the loop.

提交回复
热议问题