///<summary>
        /// Method Invocation of wrapper classes
        ///<summary>
        protected static FR_L6PA_SP_1547 Invoke(DbConnection Connection, DbTransaction Transaction, string ConnectionString, P_L6PA_SP_1547 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            bool cleanupConnection  = Connection == null;
            bool cleanupTransaction = Transaction == null;

            FR_L6PA_SP_1547 functionReturn = new FR_L6PA_SP_1547();

            try
            {
                if (cleanupConnection == true)
                {
                    Connection = CSV2Core_MySQL.Support.DBSQLSupport.CreateConnection(ConnectionString);
                    Connection.Open();
                }
                if (cleanupTransaction == true)
                {
                    Transaction = Connection.BeginTransaction();
                }

                functionReturn = Execute(Connection, Transaction, Parameter, securityTicket);

                #region Cleanup Connection/Transaction
                //Commit the transaction
                if (cleanupTransaction == true)
                {
                    Transaction.Commit();
                }
                //Close the connection
                if (cleanupConnection == true)
                {
                    Connection.Close();
                }
                #endregion
            }
            catch (Exception ex)
            {
                try
                {
                    if (cleanupTransaction == true && Transaction != null)
                    {
                        Transaction.Rollback();
                    }
                }
                catch { }

                try
                {
                    if (cleanupConnection == true && Connection != null)
                    {
                        Connection.Close();
                    }
                }
                catch { }

                throw new Exception("Exception occured in method cls_Search_Patients", ex);
            }
            return(functionReturn);
        }
 ///<summary>
 /// Opens the connection/transaction for the given connectionString, and closes them when complete
 ///<summary>
 public static FR_L6PA_SP_1547 Invoke(string ConnectionString, P_L6PA_SP_1547 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(null, null, ConnectionString, Parameter, securityTicket));
 }
 ///<summary>
 /// Invokes the method for the given Connection, and Transaction, leaving them open/not commited if no exceptions occured
 ///<summary>
 public static FR_L6PA_SP_1547 Invoke(DbConnection Connection, DbTransaction Transaction, P_L6PA_SP_1547 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(Connection, Transaction, null, Parameter, securityTicket));
 }
        protected static FR_L6PA_SP_1547 Execute(DbConnection Connection, DbTransaction Transaction, P_L6PA_SP_1547 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_L6PA_SP_1547();
            returnValue.Result = new L6PA_SP_1547();

            P_L5PA_GPfP_1242 par = new P_L5PA_GPfP_1242();
            par.PracticeID = Parameter.PracticeID;

            List <L5PA_GPfP_1242> AllPatients         = cls_Get_Patients_for_PracticeID.Invoke(Connection, Transaction, par, securityTicket).Result.ToList();
            List <L5PA_GPfP_1242> FilteredAllPatients = new List <L5PA_GPfP_1242>();

            List <L5PA_GPfP_1242a> filteredTreatments = new List <L5PA_GPfP_1242a>();
            List <L5PA_GPfP_1242b> filteredFollowUps  = new List <L5PA_GPfP_1242b>();

            //this is used for patients that do not have treatments or followups TODO: change with deep copy
            List <L5PA_GPfP_1242> AllPatients3 = cls_Get_Patients_for_PracticeID.Invoke(Connection, Transaction, par, securityTicket).Result.ToList();

            #region Quick search Today

            if (Parameter.isQuickTreatmentToday == true)
            {
                foreach (var item in AllPatients)
                {
                    filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date.ToShortDateString() == DateTime.Now.ToShortDateString() && v.TreatmentPractice_RefID == Parameter.PracticeID).ToList();
                    L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                    temp.BirthDate     = item.BirthDate;
                    temp.FirstName     = item.FirstName;
                    temp.LastName      = item.LastName;
                    temp.HEC_PatientID = item.HEC_PatientID;
                    temp.Treatments    = filteredTreatments.ToArray();

                    if (temp.Treatments.Length > 0)
                    {
                        FilteredAllPatients.Add(temp);
                    }
                }

                if (Parameter.isQuickFollowUpToday == true)
                {
                    foreach (var patient in FilteredAllPatients)
                    {
                        filteredTreatments = new List <L5PA_GPfP_1242a>();

                        foreach (var treat in patient.Treatments)
                        {
                            if (treat.FollowUp.Length > 0)
                            {
                                filteredTreatments.Add(treat);
                            }
                        }
                        if (filteredTreatments.Count > 0)
                        {
                            filteredTreatments = filteredTreatments.GroupBy(a => a.HEC_Patient_TreatmentID).Select(q => q.First()).ToList();
                            patient.Treatments = filteredTreatments.ToArray();
                        }
                        else
                        {
                            patient.Treatments = patient.Treatments.GroupBy(a => a.HEC_Patient_TreatmentID).Select(q => q.First()).ToArray();
                        }
                    }
                }
            }
            #endregion

            #region Quick search Tomorrow

            if (Parameter.isQuickTreatmentTomorrow == true)
            {
                foreach (var item in AllPatients)
                {
                    filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString() && v.TreatmentPractice_RefID == Parameter.PracticeID).ToList();
                    L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                    temp.BirthDate     = item.BirthDate;
                    temp.FirstName     = item.FirstName;
                    temp.LastName      = item.LastName;
                    temp.HEC_PatientID = item.HEC_PatientID;
                    temp.Treatments    = filteredTreatments.ToArray();

                    if (temp.Treatments.Length > 0)
                    {
                        FilteredAllPatients.Add(temp);
                    }
                }

                if (Parameter.isQuickFollowUpToday == true)
                {
                    foreach (var patient in FilteredAllPatients)
                    {
                        filteredTreatments = new List <L5PA_GPfP_1242a>();

                        foreach (var treat in patient.Treatments)
                        {
                            if (treat.FollowUp.Length > 0)
                            {
                                filteredTreatments.Add(treat);
                            }
                        }
                        if (filteredTreatments.Count > 0)
                        {
                            filteredTreatments = filteredTreatments.GroupBy(a => a.HEC_Patient_TreatmentID).Select(q => q.First()).ToList();
                            patient.Treatments = filteredTreatments.ToArray();
                        }
                        else
                        {
                            patient.Treatments = patient.Treatments.GroupBy(a => a.HEC_Patient_TreatmentID).Select(q => q.First()).ToArray();
                        }
                    }
                }
            }
            #endregion

            #region Quick search Next week

            if (Parameter.isQuickTreatmentNextWeek == true)
            {
                foreach (var item in AllPatients)
                {
                    filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date.Date < DateTime.Now.Date.AddDays(8)).ToList();
                    filteredTreatments = filteredTreatments.Where(v => v.IfSheduled_Date.Date > DateTime.Now.Date).ToList();
                    filteredTreatments = filteredTreatments.Where(v => v.TreatmentPractice_RefID == Parameter.PracticeID).ToList();

                    L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                    temp.BirthDate     = item.BirthDate;
                    temp.FirstName     = item.FirstName;
                    temp.LastName      = item.LastName;
                    temp.HEC_PatientID = item.HEC_PatientID;
                    temp.Treatments    = filteredTreatments.ToArray();

                    if (temp.Treatments.Length > 0)
                    {
                        FilteredAllPatients.Add(temp);
                    }
                }
            }
            #endregion

            #region Quick search Last week

            if (Parameter.isQuickTreatmentLastWeek == true)
            {
                foreach (var item in AllPatients)
                {
                    filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date.Date < DateTime.Now.Date).ToList();
                    filteredTreatments = filteredTreatments.Where(v => v.IfSheduled_Date.Date > DateTime.Now.AddDays(-7)).ToList();
                    filteredTreatments = filteredTreatments.Where(v => v.TreatmentPractice_RefID == Parameter.PracticeID).ToList();

                    L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                    temp.BirthDate     = item.BirthDate;
                    temp.FirstName     = item.FirstName;
                    temp.LastName      = item.LastName;
                    temp.HEC_PatientID = item.HEC_PatientID;
                    temp.Treatments    = filteredTreatments.ToArray();

                    if (temp.Treatments.Length > 0)
                    {
                        FilteredAllPatients.Add(temp);
                    }
                }
            }
            #endregion

            #region Quick search Next two weeks

            if (Parameter.isQuickTreatmentNextTwoWeeks == true)
            {
                foreach (var item in AllPatients)
                {
                    filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date.Date < DateTime.Now.Date.AddDays(15)).ToList();
                    filteredTreatments = filteredTreatments.Where(v => v.IfSheduled_Date.Date > DateTime.Now.Date).ToList();
                    filteredTreatments = filteredTreatments.Where(v => v.TreatmentPractice_RefID == Parameter.PracticeID).ToList();

                    L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                    temp.BirthDate     = item.BirthDate;
                    temp.FirstName     = item.FirstName;
                    temp.LastName      = item.LastName;
                    temp.HEC_PatientID = item.HEC_PatientID;
                    temp.Treatments    = filteredTreatments.ToArray();

                    if (temp.Treatments.Length > 0)
                    {
                        FilteredAllPatients.Add(temp);
                    }
                }
            }
            #endregion

            #region Quick Search Followup Today

            if (Parameter.isQuickFollowUpToday == true)
            {
                foreach (var item in AllPatients)
                {
                    if (FilteredAllPatients.Where(k => k.HEC_PatientID == item.HEC_PatientID).ToList().Count > 0)
                    {
                        continue;
                    }

                    if (item.Treatments.Length > 0)
                    {
                        List <L5PA_GPfP_1242a> itemTreatmentsList             = item.Treatments.ToList();
                        List <L5PA_GPfP_1242a> filteredTreatmentsForFollowups = new List <L5PA_GPfP_1242a>();

                        foreach (var treatment in itemTreatmentsList)
                        {
                            List <L5PA_GPfP_1242b> filteredFollowUps2 = new List <L5PA_GPfP_1242b>();


                            filteredFollowUps2 = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date.ToShortDateString() == DateTime.Now.ToShortDateString() && k.FollowUp_PracticeID == Parameter.PracticeID).ToList();
                            treatment.FollowUp = filteredFollowUps2.ToArray();

                            if (treatment.FollowUp.Length > 0)
                            {
                                filteredTreatmentsForFollowups.Add(treatment);
                            }
                        }

                        if (filteredTreatmentsForFollowups.Count > 0)
                        {
                            if (Parameter.isQuickTreatmentToday == true)
                            {
                                if (filteredTreatmentsForFollowups.Count > 1)
                                {
                                    filteredTreatmentsForFollowups.RemoveRange(1, filteredTreatmentsForFollowups.Count - 1);
                                }
                            }


                            L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                            temp.BirthDate     = item.BirthDate;
                            temp.FirstName     = item.FirstName;
                            temp.LastName      = item.LastName;
                            temp.HEC_PatientID = item.HEC_PatientID;
                            temp.Treatments    = filteredTreatmentsForFollowups.ToArray();

                            FilteredAllPatients.Add(temp);
                        }
                    }
                }
            }
            #endregion


            #region Quick Search Followup Tomorrow

            if (Parameter.isQuickFoolowUpTomorrow == true)
            {
                foreach (var item in AllPatients)
                {
                    if (FilteredAllPatients.Where(k => k.HEC_PatientID == item.HEC_PatientID).ToList().Count > 0)
                    {
                        continue;
                    }

                    if (item.Treatments.Length > 0)
                    {
                        List <L5PA_GPfP_1242a> itemTreatmentsList             = item.Treatments.ToList();
                        List <L5PA_GPfP_1242a> filteredTreatmentsForFollowups = new List <L5PA_GPfP_1242a>();

                        foreach (var treatment in itemTreatmentsList)
                        {
                            List <L5PA_GPfP_1242b> filteredFollowUps2 = new List <L5PA_GPfP_1242b>();


                            filteredFollowUps2 = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString() && k.FollowUp_PracticeID == Parameter.PracticeID).ToList();
                            treatment.FollowUp = filteredFollowUps2.ToArray();

                            if (treatment.FollowUp.Length > 0)
                            {
                                filteredTreatmentsForFollowups.Add(treatment);
                            }
                        }

                        if (filteredTreatmentsForFollowups.Count > 0)
                        {
                            if (Parameter.isQuickTreatmentToday == true)
                            {
                                if (filteredTreatmentsForFollowups.Count > 1)
                                {
                                    filteredTreatmentsForFollowups.RemoveRange(1, filteredTreatmentsForFollowups.Count - 1);
                                }
                            }


                            L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                            temp.BirthDate     = item.BirthDate;
                            temp.FirstName     = item.FirstName;
                            temp.LastName      = item.LastName;
                            temp.HEC_PatientID = item.HEC_PatientID;
                            temp.Treatments    = filteredTreatmentsForFollowups.ToArray();

                            FilteredAllPatients.Add(temp);
                        }
                    }
                }
            }
            #endregion

            #region Quick Search Followup Last week

            if (Parameter.isQuickFoolowUpLastWeek == true)
            {
                foreach (var item in AllPatients)
                {
                    if (item.Treatments.Length > 0)
                    {
                        List <L5PA_GPfP_1242a> itemTreatmentsList             = item.Treatments.ToList();
                        List <L5PA_GPfP_1242a> filteredTreatmentsForFollowups = new List <L5PA_GPfP_1242a>();

                        foreach (var treatment in itemTreatmentsList)
                        {
                            List <L5PA_GPfP_1242b> filteredFollowUps2 = new List <L5PA_GPfP_1242b>();


                            filteredFollowUps2 = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date.Date < DateTime.Now.Date).ToList();
                            filteredFollowUps2 = filteredFollowUps2.Where(k => k.FollowUp_IfSheduled_Date.Date > DateTime.Now.Date.AddDays(-7)).ToList();
                            filteredFollowUps2 = filteredFollowUps2.Where(k => k.FollowUp_PracticeID == Parameter.PracticeID).ToList();

                            treatment.FollowUp = filteredFollowUps2.ToArray();

                            if (treatment.FollowUp.Length > 0)
                            {
                                filteredTreatmentsForFollowups.Add(treatment);
                            }
                        }
                        if (filteredTreatmentsForFollowups.Count > 0)
                        {
                            L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                            temp.BirthDate     = item.BirthDate;
                            temp.FirstName     = item.FirstName;
                            temp.LastName      = item.LastName;
                            temp.HEC_PatientID = item.HEC_PatientID;
                            temp.Treatments    = filteredTreatmentsForFollowups.ToArray();

                            FilteredAllPatients.Add(temp);
                        }
                    }
                }
            }
            #endregion

            #region Quick Search Followup Next week

            if (Parameter.isQuickFoolowUpNextWeek == true)
            {
                foreach (var item in AllPatients)
                {
                    if (item.Treatments.Length > 0)
                    {
                        List <L5PA_GPfP_1242a> itemTreatmentsList             = item.Treatments.ToList();
                        List <L5PA_GPfP_1242a> filteredTreatmentsForFollowups = new List <L5PA_GPfP_1242a>();

                        foreach (var treatment in itemTreatmentsList)
                        {
                            List <L5PA_GPfP_1242b> filteredFollowUps2 = new List <L5PA_GPfP_1242b>();


                            filteredFollowUps2 = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date.Date < DateTime.Now.Date.AddDays(8)).ToList();
                            filteredFollowUps2 = filteredFollowUps2.Where(k => k.FollowUp_IfSheduled_Date.Date > DateTime.Now.Date).ToList();
                            filteredFollowUps2 = filteredFollowUps2.Where(k => k.FollowUp_PracticeID == Parameter.PracticeID).ToList();

                            treatment.FollowUp = filteredFollowUps2.ToArray();

                            if (treatment.FollowUp.Length > 0)
                            {
                                filteredTreatmentsForFollowups.Add(treatment);
                            }
                        }
                        if (filteredTreatmentsForFollowups.Count > 0)
                        {
                            L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                            temp.BirthDate     = item.BirthDate;
                            temp.FirstName     = item.FirstName;
                            temp.LastName      = item.LastName;
                            temp.HEC_PatientID = item.HEC_PatientID;
                            temp.Treatments    = filteredTreatmentsForFollowups.ToArray();

                            FilteredAllPatients.Add(temp);
                        }
                    }
                }
            }
            #endregion

            #region Quick Search Followup Next two weeks

            if (Parameter.isQuickFollowUpNextTwoWeeks == true)
            {
                foreach (var item in AllPatients)
                {
                    if (item.Treatments.Length > 0)
                    {
                        List <L5PA_GPfP_1242a> itemTreatmentsList             = item.Treatments.ToList();
                        List <L5PA_GPfP_1242a> filteredTreatmentsForFollowups = new List <L5PA_GPfP_1242a>();

                        foreach (var treatment in itemTreatmentsList)
                        {
                            List <L5PA_GPfP_1242b> filteredFollowUps2 = new List <L5PA_GPfP_1242b>();


                            filteredFollowUps2 = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date.Date < DateTime.Now.Date.AddDays(15)).ToList();
                            filteredFollowUps2 = filteredFollowUps2.Where(k => k.FollowUp_IfSheduled_Date.Date > DateTime.Now.Date).ToList();
                            filteredFollowUps2 = filteredFollowUps2.Where(k => k.FollowUp_PracticeID == Parameter.PracticeID).ToList();

                            treatment.FollowUp = filteredFollowUps2.ToArray();

                            if (treatment.FollowUp.Length > 0)
                            {
                                filteredTreatmentsForFollowups.Add(treatment);
                            }
                        }
                        if (filteredTreatmentsForFollowups.Count > 0)
                        {
                            L5PA_GPfP_1242 temp = new L5PA_GPfP_1242();
                            temp.BirthDate     = item.BirthDate;
                            temp.FirstName     = item.FirstName;
                            temp.LastName      = item.LastName;
                            temp.HEC_PatientID = item.HEC_PatientID;
                            temp.Treatments    = filteredTreatmentsForFollowups.ToArray();

                            FilteredAllPatients.Add(temp);
                        }
                    }
                }
            }
            #endregion

            //Search when user comes to the page, gives treatments and  followups today and all patients that do not have any treatment or followup
            if (Parameter.isQuickFollowUpToday == true && Parameter.isQuickTreatmentToday == true && Parameter.withTreatments == true)
            {
                if (Parameter.withTreatments == true)
                {
                    List <L5PA_GPfP_1242> FilteredAllPatientsNoTreatment = new List <L5PA_GPfP_1242>();

                    FilteredAllPatientsNoTreatment = AllPatients3.Where(v => v.Treatments.Length == 0).ToList();

                    FilteredAllPatientsNoTreatment = FilteredAllPatientsNoTreatment.Where(k => k.PracticeID == Parameter.PracticeID).ToList();


                    foreach (var patient in FilteredAllPatientsNoTreatment)
                    {
                        FilteredAllPatients.Add(patient);
                    }
                }
            }

            #region Noraml Search

            if (Parameter.isQuickFollowUpNextTwoWeeks == false && Parameter.isQuickFollowUpToday == false && Parameter.isQuickTreatmentNextTwoWeeks == false && Parameter.isQuickTreatmentToday == false &&
                Parameter.isQuickTreatmentLastWeek == false && Parameter.isQuickTreatmentTomorrow == false && Parameter.isQuickTreatmentNextWeek == false && Parameter.isQuickFoolowUpTomorrow == false &&
                Parameter.isQuickFoolowUpNextWeek == false && Parameter.isQuickFoolowUpLastWeek == false)
            {
                if (Parameter.withTreatments == false)
                {
                    AllPatients = AllPatients.Where(c => c.Treatments.Length > 0).ToList();
                }
                else
                {
                    AllPatients = AllPatients.Where(c => c.Treatments.Length > 0).ToList();
                    List <L5PA_GPfP_1242> FilteredAllPatientsNoTreatment = new List <L5PA_GPfP_1242>();

                    FilteredAllPatientsNoTreatment = AllPatients3.Where(v => v.Treatments.Length == 0).ToList();

                    FilteredAllPatientsNoTreatment = FilteredAllPatientsNoTreatment.Where(k => k.PracticeID == Parameter.PracticeID).ToList();


                    foreach (var patient in FilteredAllPatientsNoTreatment)
                    {
                        AllPatients.Add(patient);
                    }
                }


                if (Parameter.FirstName != "")
                {
                    foreach (var item in AllPatients)
                    {
                        if (Core_ClassLibrarySupport.Utils.StringExtensions.ContainsIgnoreCase(item.FirstName, Parameter.FirstName) == true)
                        {
                            FilteredAllPatients.Add(item);
                        }
                    }

                    AllPatients = FilteredAllPatients;
                }

                if (Parameter.LastName != "")
                {
                    FilteredAllPatients = new List <L5PA_GPfP_1242>();
                    foreach (var pat in AllPatients)
                    {
                        FilteredAllPatients.Add(pat);
                    }

                    foreach (var item in AllPatients)
                    {
                        if (Core_ClassLibrarySupport.Utils.StringExtensions.ContainsIgnoreCase(item.LastName, Parameter.LastName) == false)
                        {
                            FilteredAllPatients.Remove(item);
                        }
                    }

                    AllPatients = FilteredAllPatients;
                }

                if (Parameter.BirthDate != null)
                {
                    DateTime date = (DateTime)Parameter.BirthDate;
                    AllPatients = AllPatients.Where(c => c.BirthDate.ToShortDateString() == date.ToShortDateString()).ToList();
                }

                #region TreatmentFrom

                if (Parameter.TreatmentFrom != null)
                {
                    FilteredAllPatients = new List <L5PA_GPfP_1242>();

                    foreach (var item in AllPatients)
                    {
                        filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date > Parameter.TreatmentFrom).ToList();
                        item.Treatments    = filteredTreatments.ToArray();

                        if (item.Treatments.Length > 0)
                        {
                            FilteredAllPatients.Add(item);
                        }
                    }

                    AllPatients = FilteredAllPatients;
                }

                #endregion

                #region TreatmentTo

                if (Parameter.TreatmentTo != null)
                {
                    FilteredAllPatients = new List <L5PA_GPfP_1242>();

                    foreach (var item in AllPatients)
                    {
                        filteredTreatments = item.Treatments.Where(v => v.IfSheduled_Date < Parameter.TreatmentTo).ToList();
                        item.Treatments    = filteredTreatments.ToArray();

                        if (item.Treatments.Length > 0)
                        {
                            FilteredAllPatients.Add(item);
                        }
                    }

                    AllPatients = FilteredAllPatients;
                }

                #endregion

                #region FollowupFrom

                if (Parameter.FollowupFrom != null)
                {
                    FilteredAllPatients = new List <L5PA_GPfP_1242>();
                    filteredTreatments  = new List <L5PA_GPfP_1242a>();

                    foreach (var item in AllPatients)
                    {
                        if (item.Treatments.Length > 0)
                        {
                            List <L5PA_GPfP_1242a> itemTreatmentsList = item.Treatments.ToList();

                            foreach (var treatment in itemTreatmentsList)
                            {
                                filteredFollowUps  = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date > Parameter.FollowupFrom).ToList();
                                treatment.FollowUp = filteredFollowUps.ToArray();

                                if (treatment.FollowUp.Length > 0)
                                {
                                    filteredTreatments.Add(treatment);
                                }
                            }
                            if (filteredTreatments.Count > 0)
                            {
                                item.Treatments = filteredTreatments.ToArray();
                                FilteredAllPatients.Add(item);
                            }
                        }
                    }

                    AllPatients = FilteredAllPatients;
                }
                #endregion

                #region FollowUpTo

                if (Parameter.FollowUpTo != null)
                {
                    FilteredAllPatients = new List <L5PA_GPfP_1242>();
                    filteredTreatments  = new List <L5PA_GPfP_1242a>();

                    foreach (var item in AllPatients)
                    {
                        if (item.Treatments.Length > 0)
                        {
                            List <L5PA_GPfP_1242a> itemTreatmentsList = item.Treatments.ToList();

                            foreach (var treatment in itemTreatmentsList)
                            {
                                filteredFollowUps  = treatment.FollowUp.Where(k => k.FollowUp_IfSheduled_Date < Parameter.FollowUpTo).ToList();
                                treatment.FollowUp = filteredFollowUps.ToArray();

                                if (treatment.FollowUp.Length > 0)
                                {
                                    filteredTreatments.Add(treatment);
                                }
                            }
                            if (filteredTreatments.Count > 0)
                            {
                                item.Treatments = filteredTreatments.ToArray();
                                FilteredAllPatients.Add(item);
                            }
                        }
                    }

                    AllPatients = FilteredAllPatients;
                }

                #endregion


                if (Parameter.FirstName == "" && Parameter.LastName == "" && Parameter.BirthDate == null && Parameter.TreatmentFrom == null && Parameter.TreatmentTo == null &&
                    Parameter.FollowupFrom == null && Parameter.FollowUpTo == null)
                {
                    List <L5PA_GPfP_1242> AllPatients2 = cls_Get_Patients_for_PracticeID.Invoke(Connection, Transaction, par, securityTicket).Result.ToList();
                    List <L5PA_GPfP_1242> FilteredAllPatientsTodayTreatment = new List <L5PA_GPfP_1242>();
                    FilteredAllPatients = new List <L5PA_GPfP_1242>();



                    foreach (var item2 in FilteredAllPatients)
                    {
                        if (!FilteredAllPatientsTodayTreatment.Select(c => c.HEC_PatientID).Contains(item2.HEC_PatientID))
                        {
                            FilteredAllPatientsTodayTreatment.Add(item2);
                        }
                    }

                    AllPatients = FilteredAllPatientsTodayTreatment;

                    //if search patients without treatments and followups
                    if (Parameter.withTreatments == true)
                    {
                        List <L5PA_GPfP_1242> FilteredAllPatientsNoTreatment = new List <L5PA_GPfP_1242>();

                        FilteredAllPatientsNoTreatment = AllPatients3.Where(v => v.Treatments.Length == 0).ToList();

                        FilteredAllPatientsNoTreatment = FilteredAllPatientsNoTreatment.Where(k => k.PracticeID == Parameter.PracticeID).ToList();


                        foreach (var patient in FilteredAllPatientsNoTreatment)
                        {
                            AllPatients.Add(patient);
                        }
                    }
                }

                returnValue.Result.AllPatients = AllPatients.ToArray();
            }
            else
            {
                returnValue.Result.AllPatients = FilteredAllPatients.ToArray();
            }



            #endregion

            return(returnValue);

            #endregion UserCode
        }