pauloh
pauloh

Reputation: 1002

Modifying properties in object with LINQ and functions Lamba

Friends,

I know how to deploy and retrieve a single element in LINQ, but how can I do to change all the properties in a list. In the line below, I can only modify a record, I would modify several.

_ListaAcaoMenuInfo.Where(p => p.Id_acao == id).FirstOrDefault().Id_menu = 0;

Thanks

Upvotes: 1

Views: 183

Answers (3)

ChrisNel52
ChrisNel52

Reputation: 15143

Use the ForEach function of a List...

_ListaAcaoMenuInfo.Where(p => p.Id_acao == id).ToList().ForEach(item=>item.Id_menu=0);

Upvotes: 6

Anthony Pegram
Anthony Pegram

Reputation: 126864

You wouldn't want to. LINQ is not to be used for side effects. There's a foreach loop for that.

foreach (var x in collection.where(x => x.Foo = "Blah"))
   x.Foo = "Bar";

Upvotes: 2

Dave Swersky
Dave Swersky

Reputation: 34810

Use foreach:

var l = _ListaAcaoMenuInfo.Where(p => p.Id_acao == id).ToList();

foreach (Thing i in l)
{
   i.Id_menu = 0;
   //now use your Context object to save back to the database
}

Upvotes: 1

Related Questions