You cannot do this naturally with IEnumerable<T> , since you won’t know if there are more elements until you try to move on to the next.
However, in my MiscUtil library , I have something called SmartEnumerable that reads one element in advance to provide the following properties:
See this page for instructions on use. For instance:
For Each item In collection.ToSmartEnumerable() If item.IsFirst DoX() Else DoY() End If Next
You will need to use item.Value to get the value of the current item.
One point about iterating over dictionaries - they are actually not in any order. Therefore, while you can iterate, and there will be a first record and a last record, you should not assume that they will be the first record added and the last record respectively. This may not be a problem for your use case, but you should be aware of this.
source share