1

I have a list (MyList) of objects...and I would like to update one property (Priority) of each item in that list.

Will the below code work for that ?

this.MyList.All(
    delegate(ItemViewModel itemObject)
    {   
        itemObject.Priority = priority++;
    }
)

Please help me. Thanks in advance!

mellamokb
  • 56,094
  • 12
  • 110
  • 136
Relativity
  • 6,690
  • 22
  • 78
  • 128
  • 2
    The extension method `All()` would return true/false depending on whether each item in the list met the criteria given in the delegate. Since the delegate doesn't return a Boolean, this shouldn't compile. – neontapir Oct 12 '11 at 03:30

2 Answers2

2

Why not just use foreach?

foreach(ItemViewModel itemObject in MyList)
  itemObject.Priority = priority++;

If you really want to use a delegate you can use ForEach():

MyList.ForEach(itemObject =>
{
    itemObject.Priority = priority++;
});

This is not advisable though since you introduce a side effect with priority++

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

If it is a list, then you should use:

MyList.ForEach(i => i.Priority = priority++);

or the equivalent:

MyList.ForEach(delegate(ItemViewModel i) { i.Priority = priority++ });
Candide
  • 30,469
  • 8
  • 53
  • 60