It is very difficult for me to explain, but I need some help to create a LINQ query (in C#) that will find items with numbers that are following.
Let me explain this with an example. There's a collection of numbers:
1
5
7
8
11
1开发者_如何学JAVA2
20
I need the combination '7' & '8', and '11' & '12' because there are following logical. But how can I create a LINQ query that will return this two (or more) combinations, or at least the first number of following row (7 of 11)?
Thanks.
IEnumerable<int> items = //whatever
var pairs = items.Zip(items.Skip(1), (f, s) => Tuple.Create(f, s))
.Where(t => t.Item1 + 1 == t.Item2);
Note this only works for .Net 4 and would return two results if there were a sub-sequence of 7,8,9 for example.
See the answers to Selecting Consecutive Entries with LINQ to Entities
精彩评论