I guess it would just be a tedious loop.
1. Put all of the segments in a list.
2. Take one segment out of the list and put in into a new polyline.
3. Compare the end points of every segment in the list to the ends of the polyline.
4. If you have a match, add the segment to the polyline and take it out of the list. Go to step 3.
5. If you cannot find a match, go to step 2.
6. Do this until the list is empty.
This is assuming that all of the end points of the segments connect to either zero or one other end point. Since you are making contours, I guess this would be the case. I'm sure there are smarter and faster ways of doing this so you don't have to iterate through the list so many times. There might even be something sneaky you can do with Linq to group all of the segments that share end points.