Question: How do I remove the OBR segments that do not contain any corresponding OBX records
To do this you can loop through the OrderGroups in the message and remove all OBRs except the first one.
public override void Run() { // Get an HL7 Message in parsed format HL7Message message = GetParsedMessage(); foreach(OrderGroup order in message.GetOrderGroups()) { int end = order.OBXs.Count == 0 ? order.OBRs.Count : order.OBRs.Count -1; for(int i = 0; i < end; ++i) { OBR obr = order.OBRs[i]; message.Remove(obr); } } SaveMessage(message, "Modified"); }