How do I access consecutive elements in an IQueryable object?

Using extension methods makes this fairly easy public static class IEnumerableExtensions { public static IEnumerable> WithPrevious(this IEnumerable @this) { using (var e = @this.GetEnumerator()) { if (!e.MoveNext()) yield break; var previous = e. Current; while (e.MoveNext()) { yield return new ValueWithPrevious(e. Current, previous); previous = e.

Current; } } } } public struct ValueWithPrevious { public readonly T Value, Previous; public ValueWithPrevious(T value, T previous) { Value = value; Previous = previous; } } Usage: var array = new int { 1, 2, 3, 4, 5 }; foreach (var value in array.WithPrevious()) { Console. WriteLine("{0}, {1}", value. Previous, value.

Value); // Results: 1, 2 // 2, 3 // 3, 4 // 4, 5 }.

Using extension methods makes this fairly easy. Public static class IEnumerableExtensions { public static IEnumerable> WithPrevious(this IEnumerable @this) { using (var e = @this.GetEnumerator()) { if (!e.MoveNext()) yield break; var previous = e. Current; while (e.MoveNext()) { yield return new ValueWithPrevious(e.

Current, previous); previous = e. Current; } } } } public struct ValueWithPrevious { public readonly T Value, Previous; public ValueWithPrevious(T value, T previous) { Value = value; Previous = previous; } } Usage: var array = new int { 1, 2, 3, 4, 5 }; foreach (var value in array.WithPrevious()) { Console. WriteLine("{0}, {1}", value.

Previous, value. Value); // Results: 1, 2 // 2, 3 // 3, 4 // 4, 5 }.

You can turn an IQueryable into a List using ToList().

One thing to consider is that you lose deferred execution with this method. – Samuel Apr 2 '09 at 15:08.

EDIT Misread the question a bit. This code will give you consequetive elements public static IEnumerable> GroupIntoConsequetive(this IEnumerable enumerable) { using ( var e = enumerable.GetEnumerator() ) { if (!e.MoveNext() ) { yield break; } var last = e. Current; while ( e.MoveNext() ) { yield return new Pair(last, e.

Current); last = e. Current; } } } I'm not sure there is default way but writing an extension method to do so shouldn't be to difficult. I'm assuming there is a simple Pair implementation public static IEnumerable> Window(this IEnumerable enumerable) { using ( var e = enumerable.GetEnumerator() ) { while ( e.MoveNext() ) { var first = e.

Current; if (!e.MoveNext() ) { throw new InvalidOperationException("Need even number"); } var second = e. Current; yield return new Pair(first,second); } } } With the window you could then get the behavior you desire with the following var col = GetQueryableItem(); col.Window(). Select(pair => method1(pair.

First, pair. Second)); Quick and dirty Pair implementation public struct Pair { public readonly T1 First; public readonly T2 Second; public Pair(T1 first, T2 second) { First = first; Second = second; } }.

He wants to access the previous element, not group them into pairs. – Samuel Apr 2 '09 at 14:49.

But it provides extension methods to create an array or a list from your IQueryable instance, see ToArray() and ToList(). You can then go and do the same as you would with the array in your example.

IQueryable is IEnumerable. So you can do something like: var a = new {1, 2, 3, 4}.AsQueryable(); if (a.Count().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions