[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Updating a collection using a lambda

Paul Prewett

3/7/2008 4:20:00 PM

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.


--
-Paul Prewett
4 Answers

Jon Skeet

3/7/2008 4:33:00 PM

0

Paul Prewett <link9@community.nospam> wrote:
> 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?

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.... Blog: http://www.msmvps.com...
World class .NET training in the UK: http://iterativetrai...

Paul Prewett

3/7/2008 5:19:00 PM

0

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.


--
-Paul Prewett


"Jon Skeet [C# MVP]" wrote:

> Paul Prewett <link9@community.nospam> wrote:
> > 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?
>
> 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.... Blog: http://www.msmvps.com...
> World class .NET training in the UK: http://iterativetrai...
>

Jon Skeet

3/7/2008 5:41:00 PM

0

Paul Prewett <link9@community.nospam> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.... Blog: http://www.msmvps.com...
World class .NET training in the UK: http://iterativetrai...

Paul Prewett

3/7/2008 6:47:00 PM

0

Ah. There we go.

Thankee, sai.



--
-Paul Prewett


"Jon Skeet [C# MVP]" wrote:

> Paul Prewett <link9@community.nospam> wrote:
> > 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.... Blog: http://www.msmvps.com...
> World class .NET training in the UK: http://iterativetrai...
>