Mike
Mike

Reputation: 6050

Sum elements of the array based on index position, preferable with Linq

So I have an array

int number = {3 ,5 ,2};

I know

number.Sum() = 10

and

number.Select(x=> sum+x) => {3, 8, 10}

I am looking for a way given a index to return the sum of the elements from index i to N

Example:

Sum(0) = 10
Sum(1) = 7
Sum(2) = 2

I can easily do it with a loop but I was wondering if there was a one-statement Linq that could be used.

Upvotes: 1

Views: 6437

Answers (1)

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

With i the number of elements you want to skip:

number.Skip(i).Sum();

Upvotes: 7

Related Questions