views:

85

answers:

1

Hi,

I have a list/array and need to process certain elements, but also need the index of the element in the processing. Example:

List Names = john, mary, john, bob, simon
Names.Where(s => s != "mary").Foreach(MyObject.setInfo(s.index, "blah")

But cannot use the "index" property with lists, inversely if the names were in an Array I cannot use Foreach... Any suggestions?

+5  A: 

You should use a simple for loop, like this:

var someNames = Names.Where(s => s != "mary").ToArray();

for (int i = 0; i < someNames.Length; i++)
    someNames.setInfo(i, "blah");

LINQ is not the be-all and end-all of basic loops.

If you really want to use LINQ, you need to call Select:

Names.Where(s => s != "mary").Select((s, i) => new { Index = i, Name = s })
SLaks