title: linq query Map repeating EDI data to XML structure
body: <p>Long story (see below for details) short, does anyone know I can frame a query to return repeating data into a known xml format, so each node and child elements are returned separately for a given criteria? 
  <br />
  <strong>EDIT:</strong> I guess my question boils down to: How can I query repeating data, and get a group of entries until the next occurrence of a duplicate?
  <br />
  UNB<br />
  UNH<br />
  MID<br />
  DAN<br />
  UNS<br />
  UNT<br />
  UNH<br />
  MID<br />
  DAN<br />
  UNS<br />
  UNT<br />
  UNH<br />
  MID<br />
  DAN<br />
  UNS<br />
  UNT<br />
  UNH<br />
  MID<br />
  DAN<br />
  UNS<br />
  UNT<br />
  UNH<br />
  MID<br />
  DAN<br />
  UNS<br />
  UNT<br />
  So in this case knowing that some segments repeat, how can I write a query that would return UNH - UNT, in  groups of:
  <br />
  UNH<br />
  MID<br />
  DAN<br />
  UNS<br />
  UNT<br /></p>
  
  <p><strong>Full Details</strong>
  I have a requirement to write a generic edi (EDIFACT) to xml mapper. Since the xml is structured, and known I have designed the application to import an xml file, extract and store the structure of the file using entity framework.<br /><img src="https://i.stack.imgur.com/j6ExY.jpg" alt="enter image description here">
  The Xml structure is stored as a self referencing entity called Translation. The translation contains the xml node, the Edifact segment name it maps to and the position of the composite and element to extract the value from. The translation also references itself as a parent/child association (to mirror the xml structure). The UI automatically matches content in each node to the segment, and composite and element positions.
  The problem I have is how to separate repeating data into the appropriate repeating parent nodes:
  <img src="https://i.stack.imgur.com/Xgz5Z.jpg" alt="Desired XML structure on right, my output on left">
  I am building this by extracting translations without parents, then walking down the tree recursively:</p>
  
  <pre><code>var rootTrans = (from t in Translations
                      where t.ParentID==null
                      select t).ToList();
  //(msg is the EDI transmission with a collection of segments-each segment contains composites, each composite contains elements with the actual data eg- )
  
  XElement root = new XElement(Tag.Replace(" ", ""));
  foreach (var item in rootTrans)
  {
      item.AddContentRecursive(root, msg);
  }
  
  var doc = new XDocument(root);
  
  
  
  // AddContentRecursive
  public void AddContentRecursive(XElement element, Transmission msg)
  {
      // Find matching segments for this tag
      var segNodes = (from s in msg.Segments
                          where s.Value == Name
                          select GetElementValue(s, Positions)).DefaultIfEmpty(null);
      // simply add the matching nodes to the parent element
      if (Children.Count == 0)
      {
          var nodes = (from t in segNodes
                          select new XElement(Tag, t));
          element.Add(nodes);
      }
      else
      {
          var aNode = new XElement(Tag);
          element.Add(aNode);
          foreach (var chil in Children.OrderBy(c =&gt; c.DocumentOrder))
          {
              chil.AddContentRecursive(aNode, msg);
          }
      }
  }
  </code></pre>
  
  <p>The EDI files are controlled by customers, and follow their own pattern, from the example below the UNH segment is a message header, and will repeat until a UNT. The translation entity for UNH has a property called tag (MessageHeader) and one called Name (UNH):</p>
  
  <blockquote>
    <p>UNH+ORDERR319952+ORDERR:1::OD'<br /> MID+ORDERR319952+120818'<br />
    DAN+:ADVICE NOTE+TGBGU649'<br /> MSD+CR'<br /> SDT+0831824'<br />
    BDT+PIPUK+++++RB'<br /> ART++46210JD000:::::PIPE WORK,RH'<br />
    DLV++2160:45'<br /> DTP+120823:1700'<br /> PIN+21'<br />
    TCO+:BLB+24+90'<br /> CSG+0831824+F01-XJ10'<br /> UNS+S'<br />
    UNT+14+ORDERR319952'<br /> UNH+ORDERR340246+ORDERR:1::OD'<br />
    MID+ORDERR340246+120818'<br /> DAN+:ADVICE NOTE+RRHBU636'<br />
    MSD+CR'<br /> SDT+0831824'<br /> BDT+PIPUK+++++RB'<br />
    ART++46210JD005:::::PIPE WORK,LH'<br /> DLV++2160:45'<br />
    DTP+120823:1700'<br /> PIN+21'<br /> TCO+:BLB+24+90'<br />
    CSG+0831824+F01-XJ10'<br /> UNS+S'<br /> UNT+14+ORDERR340246'<br />
    UNH+ORDERR340247+ORDERR:1::OD'<br /> MID+ORDERR340247+120818'<br />
    DAN+:ADVICE NOTE+RRHBU641'<br /> MSD+CR'<br /> SDT+0831824'<br />
    BDT+PIPUK+++++RB'<br /> ART++46210JD600:::::PIPE WORK,RH'<br />
    DLV++4200:45'<br /> DTP+120823:1700'<br /> PIN+21'<br />
    TCO+:BAC4325+28+150'<br /> CSG+0831824+F01-XJ10'<br /> UNS+S'<br />
    UNT+14+ORDERR340247'<br /></p>
  </blockquote>
  
  <p>I have tried different combinations of queries, recursion, loops, and I know I'm missing something elementary, I'm in brick wall territory. Has anyone encountered this sort of problem? Any suggestions toward shaping the query, will be appreciated.
  Thanks</p>