I have a list of objects which all have an id property
E.g
1, 10, 25, 30, 4
I have a currentId and I need to find the next Id in the list
So
There are quite a few solution. I suggest something like the following.
var next = items
.Where(item => item.Id > currentId)
.OrderBy(item => item.Id)
.First();
var next = items
.OrderBy(item => item.Id)
.First(item => item.Id > currentId);
If you want the ids in the order they appear in the collection, you could use the following.
var next = items
.SkipWhile(item => item.Id != currentId)
.Skip(1)
.FirstOrDefault();
If this returns null, you have tried to get next item of the last item.