jeremychan
jeremychan

Reputation: 4459

argument in for each

My original line is

foreach(var file in files.OrderByDescending(x=>x).Skip(7)

I would like to replace the "7" with an argument of a commandline:

foreach(var file in files.OrderByDescending(x=>x).Skip({0}), args[1])

this is having wrong syntax?

Upvotes: 0

Views: 113

Answers (2)

Dennis Traub
Dennis Traub

Reputation: 51644

int number;

if (!int.TryParse(args[1], out number))
    throw new ArgumentException("The entered parameter is not a number.");

foreach(var file in files.OrderByDescending(x=>x).Skip(number)))
{
    // Whatever you do with each file
}

int.TryParse returns false if the given argument string is not a number.

Upvotes: 2

Alastair Pitts
Alastair Pitts

Reputation: 19601

Assuming that args[1] is a string, what you want is:

foreach(var file in files.OrderByDescending(x=>x).Skip(int.Parse(args[1])))

it should be noted there is no error checking here, so if args[1] is not a number, you will get unhandled exception.

Upvotes: 3

Related Questions