Thank you everyone my replies are ( in order of the responses gmail shows me ):
@Michael : Extension method added @James : Totally had forgotten about RX alternative, will be digging into it. @Bill : I was trying to avoid using ForEach ( without having any logical reason, Michael's and Nathan's answers both seem more appealing than using ForEach ) @Nathan : Nice Thank you all Regards Arjang On 25 February 2011 13:47, Nathan Schultz <[email protected]> wrote: > List<T> already has the foreach method. > So you can do this: > > nameList.Where(p => p.FirstName == "Pater") > .ToList() > .ForEach(p => p.FirstName = "Peter"); > > > > On Fri, Feb 25, 2011 at 10:22 AM, James Chapman-Smith > <[email protected]> wrote: >> >> Hi Arjang, >> >> Have you installed the `Reactive Extensions for .Net (Rx)` >> (http://social.msdn.microsoft.com/Forums/en-US/rx/threads)? >> >> It gives you, not only the support for `IObservable<T>/IObserver<T>` for >> push collections and a back-port of the TPL for .NET 3.5SP1, but it also >> gives you a ton of extensions for `IEnumerable<T>`. >> >> You get two extension methods that answer your question. >> >> `.Run(Action<T>)` lets you write this code: >> >> var names = >> from p in nameList >> where p.FirstName == "Pater" >> select p; >> >> names.Run(p => p.FirstName = "Peter"); >> >> `.Do(Action<T>)` works like `.Run(Action<T>)` but it returns the list so >> can >> be used inside a LINQ query like this: >> >> var names = >> (from p in nameList >> where p.FirstName == "Pater" >> select p).Do(p => p.FirstName = "Peter"); >> >> or: >> >> var names = nameList >> .Where(p => p.FirstName == "Pater") >> .Do(p => p.FirstName = "Peter"); >> >> Cheers. >> >> James. >> >> -----Original Message----- >> From: [email protected] [mailto:[email protected]] >> On Behalf Of Arjang Assadi >> Sent: Friday, 25 February 2011 12:07 >> To: ozDotNet >> Subject: Linq (To Objects) Update ( like sql ) >> >> When using Linq to Objects, is it possible to change a property value >> on all the selected values? >> I am NOT using Linq to sql, I have list items and want to change value >> of some properties depending on names e.g. >> >> //typo in name entry, can not change the persisted data, instead >> correcting in code, there are no ID's to create a correction table for >> specific records, just having to hard code it heuristics. >> >> var names = from p in nameList >> where p.FirstName == "Pater" >> set p.FirstName = "Peter" >> select p; >> >> Regards >> >> Arjang >> > >
