Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

Updating a collection using a lambda

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Prewett - 07 Mar 2008 16:20 GMT
Hi -

I have a situation where I'd like to use a lambda, but I'm not sure how.  
Basically, I have a collection of objects and I want to set a property on
each object that meets a condition.

Right now, I have this:

IEnumerable<VariableCode> codes =
  variable.VariableCodeList.Where(c => c.Type == "MyCondition");
foreach (VariableCode code in codes)
{
  code.IsShown = true;
}//foreach

I'm thinking that I can replace the foreach bit with a single lambda
expression.  Can anyone help me with that syntax?

Thanks.

Signature

-Paul Prewett

Jon Skeet [C# MVP] - 07 Mar 2008 16:33 GMT
> I have a situation where I'd like to use a lambda, but I'm not sure how.  
> Basically, I have a collection of objects and I want to set a property on
[quoted text clipped - 11 lines]
> I'm thinking that I can replace the foreach bit with a single lambda
> expression.  Can anyone help me with that syntax?

Well, if you use List<T> instead, you can use List.ForEach.

Alternatively, you could (somewhat evilly) do:

var codes = from variable.VariableCodeList
           where c.Type=="MyCondition"
           select { c.IsShown=true; return c; } ;

(In other words, make the change part of the projection.)

Or you could write a ForEach extension method yourself - it would be
trivial to do.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Paul Prewett - 07 Mar 2008 17:19 GMT
Hm, yes.

Well, VariableCodeList is actually a List<T>, but once I do the Where(), I
am stuck with IEnumerable<T>.

I'm thinking I'll just leave it the way that it is.

Thanks for the input, though.  

Signature

-Paul Prewett

> > I have a situation where I'd like to use a lambda, but I'm not sure how.  
> > Basically, I have a collection of objects and I want to set a property on
[quoted text clipped - 24 lines]
> Or you could write a ForEach extension method yourself - it would be
> trivial to do.
Jon Skeet [C# MVP] - 07 Mar 2008 17:40 GMT
> Well, VariableCodeList is actually a List<T>, but once I do the Where(), I
> am stuck with IEnumerable<T>.

You can always call ToList() to get another List if you want.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Paul Prewett - 07 Mar 2008 18:47 GMT
Ah.  There we go.

Thankee, sai.

Signature

-Paul Prewett

> > Well, VariableCodeList is actually a List<T>, but once I do the Where(), I
> > am stuck with IEnumerable<T>.
>
> You can always call ToList() to get another List if you want.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.