KiRa
KiRa

Reputation: 924

Linq update multiple columns

This is the first time I saw this kind of coding style (I admit it)

(from p in Context.person_account_portfolio 
 where p.person_id == personId select p).ToList()
       .ForEach(x => x.is_default = false);

As I understand the code correctly it will update only 1 column right?.

Question: How to update many column using craig code?. Let say 2 or more columns

My way of updating a column in c# is something like this

var qry = (from a in db.Table
           where a.Id == paramId
           select a).ToList();

        foreach (var item in qry) {
            item.Name = "John";
            item.LastName = "Dela Cruz";
            . . . . 
            db.SaveChanges();
        }

Code from Craig

Upvotes: 1

Views: 5878

Answers (1)

qxg
qxg

Reputation: 7036

It's not a LINQ question. Instead it's a delegate/lambda question.

Just use this code inside the ForEach

(x) =>
{
    x.Name = "John";
    x.LastName = "Dela Cruz";
    . . . . 
    db.SaveChanges();
}

Note the open/close curly braces.

Upvotes: 3

Related Questions