Monochromie
Monochromie

Reputation: 449

LINQ query... How to perform?

Say I got next sequence:

1
2
3
4
5
6
7
8
9
10

I need make next thing with Linq:

1,2;
2,3;
3,4;
4,5;
...
9,10;

Can't get it.

Upvotes: 0

Views: 52

Answers (3)

cuongle
cuongle

Reputation: 75306

Is this what you want?

 var list = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

 var result = Enumerable.Range(0, list.Length - 1)
                        .Select(i => new[] {list[i], list[i + 1]});

Upvotes: 5

Jan P.
Jan P.

Reputation: 3297

var res =
    Enumerable.Range(1, n).Select(item => new[] { item, item + 1 });

Upvotes: 0

ptkvsk
ptkvsk

Reputation: 2222

your_sequence.Take(high - low).Select(i => new []{i, i + 1})

In your case: low = 1, high = 10.

To test you can write

Enumerable.Range(1, 10).Take(10 - 1).Select(i => new []{i, i + 1})

in LinqPad

Upvotes: 0

Related Questions