Answered by:
foreach loop how to find previous and next item while on current item

Question
-
Hi ,
I need to loop in foreach loop next item and previous item when i am on current item.
can we see next item?
Example
foreach(Data d in Datalist)
{
d.Name = compare with next item?
}
Thanks
A-
Ashok
- Edited by Jumpingboy Wednesday, November 14, 2012 9:37 PM
Wednesday, November 14, 2012 9:36 PM
Answers
-
That's not possible, at least not in the general case.
One option is to transform the loop from a `foreach` to a `for`, in which case you could use `i-1` and `i+1` as the previous and next (just adjust your loop bounds so that you don't get out of bounds errors).
If you really must use a `foreach` then the best you'll be able to do is store the two previous iterations, and always do your processing on the previous value, using the value from two iterations ago as "previous" and the "current" value as the "next".
var values = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; int i = 0; int previous = 0; int current = 0; foreach (int nextValue in values) { if (i >= 2) { //do stuff using next, previous, and current } previous = current; current = nextValue; i++; }
If this is something you do a lot you could use a more generic method to do the grouping for you:
public static IEnumerable<Tuple<T, T, T>> GroupAdjacentValues<T>(IEnumerable<T> source) { int i = 0; T previous = default(T); T current = default(T); foreach (T next in source) { //skip the first two iterations if (i >= 2) { //do stuff using next, previous, and current yield return Tuple.Create(previous, current, next); } previous = current; current = next; i++; } }
- Edited by servy42 Wednesday, November 14, 2012 9:52 PM
- Proposed as answer by Ante Meridian Wednesday, November 14, 2012 10:08 PM
- Marked as answer by Bob ShenModerator Friday, November 23, 2012 5:49 AM
Wednesday, November 14, 2012 9:48 PM -
Change your foreach to a for statement = it will solve all your problems.
foreach(Data d in Datalist) { d.Name = compare with next item? } // becomes for(int idx = 0; idx < Datalist.Count; idx ++) { if(Datalist.Item[idx] == Datalist.Item[idx+1] { } }
The code is from the top of my head and has not been tested - but it is close and will fix your problems the easy way.Digital Forensic Software Developer
CCS LABS Digital Forensic Software
Mark as Answer or Vote up if useful thank you!- Proposed as answer by Dave A Gordon Saturday, November 17, 2012 11:03 AM
- Marked as answer by Bob ShenModerator Friday, November 23, 2012 5:49 AM
Saturday, November 17, 2012 11:03 AM
All replies
-
That's not possible, at least not in the general case.
One option is to transform the loop from a `foreach` to a `for`, in which case you could use `i-1` and `i+1` as the previous and next (just adjust your loop bounds so that you don't get out of bounds errors).
If you really must use a `foreach` then the best you'll be able to do is store the two previous iterations, and always do your processing on the previous value, using the value from two iterations ago as "previous" and the "current" value as the "next".
var values = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; int i = 0; int previous = 0; int current = 0; foreach (int nextValue in values) { if (i >= 2) { //do stuff using next, previous, and current } previous = current; current = nextValue; i++; }
If this is something you do a lot you could use a more generic method to do the grouping for you:
public static IEnumerable<Tuple<T, T, T>> GroupAdjacentValues<T>(IEnumerable<T> source) { int i = 0; T previous = default(T); T current = default(T); foreach (T next in source) { //skip the first two iterations if (i >= 2) { //do stuff using next, previous, and current yield return Tuple.Create(previous, current, next); } previous = current; current = next; i++; } }
- Edited by servy42 Wednesday, November 14, 2012 9:52 PM
- Proposed as answer by Ante Meridian Wednesday, November 14, 2012 10:08 PM
- Marked as answer by Bob ShenModerator Friday, November 23, 2012 5:49 AM
Wednesday, November 14, 2012 9:48 PM -
Don't know if this helps but with linq you could do something like
Datalist.Skip(1) .Select((current, index) > { // current is the current value // nums[index] is the previous value });
Paul Linton
Wednesday, November 14, 2012 11:07 PM -
Don't know if this helps but with linq you could do something like
Datalist.Skip(1) .Select((current, index) > { // current is the current value // nums[index] is the previous value });
Thursday, November 15, 2012 2:46 PM -
Change your foreach to a for statement = it will solve all your problems.
foreach(Data d in Datalist) { d.Name = compare with next item? } // becomes for(int idx = 0; idx < Datalist.Count; idx ++) { if(Datalist.Item[idx] == Datalist.Item[idx+1] { } }
The code is from the top of my head and has not been tested - but it is close and will fix your problems the easy way.Digital Forensic Software Developer
CCS LABS Digital Forensic Software
Mark as Answer or Vote up if useful thank you!- Proposed as answer by Dave A Gordon Saturday, November 17, 2012 11:03 AM
- Marked as answer by Bob ShenModerator Friday, November 23, 2012 5:49 AM
Saturday, November 17, 2012 11:03 AM -
Thanks for reply,
but i think this ll fail when u r on last item?
actually this is same what i am doing , but thought better then layman apporach.
Thanks
A-
Ashok
Tuesday, November 20, 2012 9:51 PM -
Hi ashokapex,
I temporarily mark servy42's reply and Dave's reply as answers. You can unmark them if they provide no help.
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
Friday, November 23, 2012 5:49 AMModerator