Question:
How can we get the subset of messages for which there is more than one MRN associated with an account number?
Answer:
The best way to do this is to use a dictionary and track each message received based on the account number. At the end of processing all messages, check to see if any account has more than 1 MRN in its list. See the code below.
// Called once or many times based on RunTypeRequested
public class Tracker
{
private readonly List<string> m_medicalRecordNumbers = new List<string>(1);
public string AccountNumber {get;set;}
public List<string> MedicalRecordNumbers { get{return m_medicalRecordNumbers;} }
}
private readonly Dictionary<string,Tracker> m_items = new Dictionary<string,Tracker>(StringComparer.OrdinalIgnoreCase);
// Called once before the first message is processed.
// It is a good place to initialize objects.
// Always called from the UI thread.
public override void OnStart()
{
m_items.Clear();
}
public override void Run()
{
// Get an HL7 Message in parsed format
HL7Message message = GetParsedMessage();
PID pid = message.GetFirstSegment<PID>();
if(pid==null)
return; // no pid segment
var accountNumber = pid.PatientAccountNumber_18.Value;
if(string.IsNullOrEmpty(accountNumber))
return; // no account number
var mrn = pid.PatientIdentifierList_03.First.Value;
Tracker tracker;
if(!m_items.TryGetValue(accountNumber, out tracker))
{
tracker = new Tracker{AccountNumber = accountNumber};
m_items.Add(accountNumber, tracker);
}
if(!tracker.MedicalRecordNumbers.Any(m=>string.Compare(m, mrn, true)==0))
tracker.MedicalRecordNumbers.Add(mrn);
}
// Called once after the last message has been processed.
// It is a good place to perform cleanup and to report information.
// Always called from the UI thread.
public override void OnFinish()
{
string path = Path.GetTempFileName();
using(var stream = new System.IO.StreamWriter(path))
{
List<Tracker> itemsWithMultipleMrns = m_items.Values.Where(i=>i.MedicalRecordNumbers.Count > 1).ToList();
if(itemsWithMultipleMrns.Count == 0)
{
stream.WriteLine("No Account numbers were found with duplicate MRNs");
} else
{
foreach(Tracker item in itemsWithMultipleMrns)
{
stream.Write(item.AccountNumber);
foreach(var mrn in item.MedicalRecordNumbers)
{
stream.Write(" ,");
stream.Write(mrn);
}
stream.WriteLine();
}
}
// show the results in notepad
try
{
System.Diagnostics.Process.Start("Notepad", path);
} catch(Exception ex)
{
this.Log(Severity.Error, string.Format("Could not open file '{0}' with notepad",path));
}
}
}