private CustomerOrderLine build_line(List<Segment> lst, SegmentLabel terminator)
        {
            var nextSeg = lst.FirstOrDefault();
            var segmentCount = 0;
            if (nextSeg == null) return null;
            if (nextSeg.Label.Text == terminator.Text) return null;
            CustomerOrderLine lineItem = _poParser.CreateLine(nextSeg);
            if (lineItem == null) return null;
            lst.Remove(nextSeg);

            lineItem.ItemId = "";

            IEnumerable<Segment> remainingSegments = lst.TakeWhile(seg => seg.Label == SegmentLabel.ReferenceLabel ||
                                                                           seg.Label == SegmentLabel.ScheduleLabel);

            var line = remainingSegments.BuildInnerResponse(lineItem.LineNumber);

            segmentCount += line.ResponseElements.Count();

            line.ResponseElements.Add(new LineResponseElementEntity { ElementName = "PO1", Qualifier = "IN", Value = lineItem.CustomerPartNumber.SafeTrim() });

            _lines.Add(line);

            segmentCount += process_schedules(remainingSegments);

            lst.RemoveRange(0, segmentCount);

            _poParser.SegmentCount += segmentCount;

            return lineItem;
        }
        private static EdiSegmentCollection extract(EdiSegmentCollection segments, SegmentLabel labelType, SegmentLabel closeType, int ndx)
        {
            IList<Segment> lst;
            var segs = segments.SegmentList.ToList();

            lst = getPreviousSegmentsToStrip(segments, closeType, ndx, labelType);

            lst.ToList().ForEach(segment => segs.Remove(segment));

            if (segs.Count == 0)
                throw new Exception(string.Format("{0} label was missing!", labelType));

            var close_seg = segs.First();

            if(ndx > 0)
                segs.Remove(close_seg);

            lst = segs.TakeWhile(s => s.Label.Value != closeType.Value).ToList();

            lst.ToList().ForEach(segment => segs.Remove(segment));

            if (segs.Count == 0)
                throw new Exception(string.Format("{0} label was missing!", labelType));

            close_seg = segs.First();

            if (close_seg.Label != closeType)
                throw new Exception("This should never happen!");

            lst.Add(close_seg);
            return new EdiSegmentCollection(lst,segments.ElementDelimiter);
        }
 public static bool MoveSegmentByLabel(List<Segment> source, List<Segment> dest, SegmentLabel lbl)
 {
     Segment temp_seg = source.FindSegmentByLabel(lbl);
     if (temp_seg == null) return false;
     dest.Add(temp_seg);
     source.Remove(temp_seg);
     return true;
 }
        private static IList<Segment> getPreviousSegmentsToStrip(EdiSegmentCollection segments, SegmentLabel closeType, int ndx, SegmentLabel labelType)
        {
            IList<Segment> lst;
            try
            {
                int counter = 0;
                lst = segments.SegmentList.TakeWhile(s =>
                                                         {
                                                             if (s.Label == closeType)
                                                                 counter++;
                                                             return counter < ndx;
                                                         }).ToList();

            }
            catch
            {
                throw new Exception(string.Format("{0} label was missing!", labelType));
            }
            return lst;
        }
        private IEnumerable<IEdiInContainer> split(EdiSegmentCollection segments, SegmentLabel labelType, 
            SegmentLabel closeType, IEdiInContainer parentContainer)
        {
            IList<IEdiInContainer> returnList = new List<IEdiInContainer>();
            IList<Segment> segs = parentContainer.InnerSegments.ToList();
            segs =  RemoveNullSegments(segs);
            var ctxtCount = segs.Where(s => s != null).ToList().Count(s => s.Label == labelType);
            for (int i = 0; i < ctxtCount; i++)
            {

                returnList.Add(parentContainer.CreateChild(extract(new EdiSegmentCollection(segs, segments.ElementDelimiter), labelType, closeType, i)));
            }
            return returnList;
        }
 public static IEnumerable<Segment> FindSegmentsByLabel(this IEnumerable<Segment> segs,
                                  SegmentLabel labelValue)
 {
     return segs.Where(seg => seg.Label == labelValue);
 }
 public static Segment FindSegmentByLabel(this IEnumerable<Segment> segs, 
                                          SegmentLabel labelValue)
 {
     return segs.Find(seg =>  seg.Label == labelValue);
 }
 private static void build_list_list(Segment seg, SegmentLabel looplabel, ref List<Segment> subList, List<List<Segment>> listList)
 {
     if(seg.Label == looplabel && subList.Count > 0)
     {
         listList.Add(subList);
         subList = new List<Segment>();
     }
     subList.Add(seg);
 }
 public Segment FirstWith(SegmentLabel label)
 {
     return SegmentList.FindSegmentByLabel(label);
 }