Says I have this List : 1, 3, 5, 7, 9, 13
For example, given value is : 9, the previous item is 7 and the next item is 13
How can I achieve this using C#?
Following may be helpful
int NextValue = 0;
int PreviousValue =0;
int index = lstOfNo.FindIndex(nd =>nd.Id == 9);
var Next = lstOfNo.ElementAtOrDefault(index + 1);
var Previous = lstOfNo.ElementAtOrDefault(index - 1);
if (Next != null)
NextValue = Next;
if (Previous != null)
PreviousValue = Previous;