How do I copy the value in HL7 field OBX[1]-14.1 to HL7 field OBR[1]-7.1?

Question:

I have a file of hundreds of HL7 messages and would like to copy the value in HL7 field OBX[1]-14.1 to HL7 field OBR[1]-7.1.  Basically, I want to overwrite the contents of field OBR-7 with the contents of field OBX-14 on every message.

Answer: 

The following code will loop through all the OBR segments, and set OBR-17 to the value of the first OBX segment following the current OBR segment.

public override void Run() 
{
  HL7Message message = GetParsedMessage();
  foreach(OBR obr in message.Segments.OfType<OBR>())
  {
    OBX obx = obr.Segment.GetRelative<OBX>(1); // get the next OBX
    if(obx != null)
      obr[17] = obx[14];
  }
  
  SaveMessage(message.ToString(), "Result");
 }