///<summary>Generates one encounter xml entry and uses the global writer _x instead of the main writer _w.</summary>
		private static void GenerateEncounterEntry(EhrCqmEncounter encCur) {
			_isWriterW=false;
			Start("entry","typeCode","DRIV");
			Start("encounter","classCode","ENC","moodCode","EVN");
			_x.WriteComment("Encounter Activities Template");
			TemplateId("2.16.840.1.113883.10.20.22.4.49");
			_x.WriteComment("Encounter Performed Template");
			TemplateId("2.16.840.1.113883.10.20.24.3.23");
			StartAndEnd("id","root",_strOIDInternalCQMRoot,"extension",CqmItemAbbreviation.Enc.ToString()+encCur.EhrCqmEncounterNum.ToString());
			Start("code");
			Attribs("code",encCur.CodeValue,"displayName",encCur.Description,"codeSystem",encCur.CodeSystemOID,"codeSystemName",encCur.CodeSystemName);
			_x.WriteAttributeString("sdtc","valueSet",null,encCur.ValueSetOID);
			End("code");
			_x.WriteElementString("text","Encounter, Performed: "+encCur.ValueSetName);
			StartAndEnd("statusCode","code","completed");
			_x.WriteComment("Length of Stay");
			Start("effectiveTime");
			DateElement("low",encCur.DateEncounter);
			DateElement("high",encCur.DateEncounter);
			End("effectiveTime");
			End("encounter");
			End("entry");
			_isWriterW=true;
		}
		///<summary>The string command will retrieve all unique encounters in the date range, for the provider (based on provider.EhrKey, so may be more than one ProvNum), with age limitation or other restrictions applied.  The encounters will then be required to belong to the value sets identified by the oneOf and twoOf lists of OID's (Object Identifiers), and the patient will have to have had one or more of the oneOf encounters or two or more of the two of encounters in the list returned by the string command.  We will return a dictionary with PatNum as the key that links to a list of all EhrCqmEncounter objects for that patient with all of the required elements for creating the QRDA Category I and III documents.</summary>
		private static Dictionary<long,List<EhrCqmEncounter>> GetEncountersWithOneOfAndTwoOfOIDs(string command,List<string> listOneOfEncOIDs,List<string> listTwoOfEncOIDs) {
			Dictionary<long,List<EhrCqmEncounter>> retval=new Dictionary<long,List<EhrCqmEncounter>>();
			List<Encounter> listEncs=Crud.EncounterCrud.SelectMany(command);
			if(listEncs.Count==0) {
				return retval;
			}
			List<EhrCode> listOneOfEncs=EhrCodes.GetForValueSetOIDs(listOneOfEncOIDs,false);
			List<EhrCode> listTwoOfEncs=EhrCodes.GetForValueSetOIDs(listTwoOfEncOIDs,false);
			Dictionary<long,int> dictPatNumAndTwoOfCount=new Dictionary<long,int>();
			List<long> listPatNums=new List<long>();
			Dictionary<long,EhrCode> dictEncNumEhrCode=new Dictionary<long,EhrCode>();
			//Remove any encounters that are not one of the allowed types and create a list of patients who had 1 or more of the OneOf encounters and a dictionary with PatNum,Count
			//for counting the number of TwoOf encounters for each patient
			for(int i=listEncs.Count-1;i>-1;i--) {
				bool isOneOf=false;
				for(int j=0;j<listOneOfEncs.Count;j++) {
					if(listEncs[i].CodeValue==listOneOfEncs[j].CodeValue && listEncs[i].CodeSystem==listOneOfEncs[j].CodeSystem) {
						if(!listPatNums.Contains(listEncs[i].PatNum)) {
							listPatNums.Add(listEncs[i].PatNum);
						}
						dictEncNumEhrCode.Add(listEncs[i].EncounterNum,listOneOfEncs[j]);
						isOneOf=true;
						break;
					}
				}
				if(isOneOf) {
					continue;
				}
				bool isTwoOf=false;
				for(int j=0;j<listTwoOfEncs.Count;j++) {
					if(listEncs[i].CodeValue==listTwoOfEncs[j].CodeValue && listEncs[i].CodeSystem==listTwoOfEncs[j].CodeSystem) {
						if(dictPatNumAndTwoOfCount.ContainsKey(listEncs[i].PatNum)) {
							dictPatNumAndTwoOfCount[listEncs[i].PatNum]++;
						}
						else {
							dictPatNumAndTwoOfCount.Add(listEncs[i].PatNum,1);
						}
						dictEncNumEhrCode.Add(listEncs[i].EncounterNum,listTwoOfEncs[j]);
						isTwoOf=true;
						break;
					}
				}
				if(!isTwoOf) {//not oneOf or twoOf encounter, remove from list
					listEncs.RemoveAt(i);//not an eligible encounter
				}
			}
			//add the patients who had 2 or more of the TwoOf encounters to the list of patients
			foreach(KeyValuePair<long,int> kpairCur in dictPatNumAndTwoOfCount) {
				if(listPatNums.Contains(kpairCur.Key)) {
					continue;
				}
				if(kpairCur.Value>1) {
					listPatNums.Add(kpairCur.Key);
				}
			}
			//remove any encounters from the list for patients who did not have a OneOf or two or more of the TwoOf encounters.
			for(int i=listEncs.Count-1;i>-1;i--) {
				if(!listPatNums.Contains(listEncs[i].PatNum)) {
					listEncs.RemoveAt(i);
				}
			}
			//listEncs is now all encounters returned by the command (should be date restricted, age restricted, provider restricted, etc.) that belong to the OneOf or TwoOf list
			//for patients who had one or more of the OneOf encounters and/or two or more of the TwoOf encounters
			//dictEncNumEhrCode links an EncounterNum to an EhrCode object.  This will be an easy way to get the ValueSetOID, ValueSetName, and CodeSystemOID for the encounter.
			for(int i=0;i<listEncs.Count;i++) {
				EhrCqmEncounter ehrEncCur=new EhrCqmEncounter();
				ehrEncCur.EhrCqmEncounterNum=listEncs[i].EncounterNum;
				ehrEncCur.PatNum=listEncs[i].PatNum;
				ehrEncCur.ProvNum=listEncs[i].ProvNum;
				ehrEncCur.CodeValue=listEncs[i].CodeValue;
				ehrEncCur.CodeSystemName=listEncs[i].CodeSystem;
				ehrEncCur.DateEncounter=listEncs[i].DateEncounter;
				EhrCode ehrCodeCur=dictEncNumEhrCode[listEncs[i].EncounterNum];
				ehrEncCur.ValueSetName=ehrCodeCur.ValueSetName;
				ehrEncCur.ValueSetOID=ehrCodeCur.ValueSetOID;
				ehrEncCur.CodeSystemOID=ehrCodeCur.CodeSystemOID;
				string descript="";
				descript=ehrEncCur.Description;//in case not in table default to EhrCode object description
				//to get description, first determine which table the code is from.  Encounter is only allowed to be a CDT, CPT, HCPCS, and SNOMEDCT.
				switch(ehrEncCur.CodeSystemName) {
					case "CDT":
						descript=ProcedureCodes.GetProcCode(ehrEncCur.CodeValue).Descript;
						break;
					case "CPT":
						Cpt cptCur=Cpts.GetByCode(ehrEncCur.CodeValue);
						if(cptCur!=null) {
							descript=cptCur.Description;
						}
						break;
					case "HCPCS":
						Hcpcs hCur=Hcpcses.GetByCode(ehrEncCur.CodeValue);
						if(hCur!=null) {
							descript=hCur.DescriptionShort;
						}
						break;
					case "SNOMEDCT":
						Snomed sCur=Snomeds.GetByCode(ehrEncCur.CodeValue);
						if(sCur!=null) {
							descript=sCur.Description;
						}
						break;
				}
				ehrEncCur.Description=descript;
				if(retval.ContainsKey(ehrEncCur.PatNum)) {
					retval[ehrEncCur.PatNum].Add(ehrEncCur);
				}
				else {
					retval.Add(ehrEncCur.PatNum,new List<EhrCqmEncounter>() { ehrEncCur });
				}
			}
			return retval;
		}