public async Task <bool> PostRequestNewInsurance(string practiceId, Insurance insurance, string userCreatedBy)
        {
            SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
            int    value  = 275380002;
            Entity entity = new Entity(Account.EntityLogicalName);

            entity.Attributes["mzk_accounttype"]    = new OptionSetValue(value);
            entity.Attributes["mzk_usercreatedby"]  = userCreatedBy; //Addition
            entity.Attributes["mzk_usermodifiedby"] = userCreatedBy; //Addition
            entity.Attributes["name"]                     = insurance.insuranceName;
            entity.Attributes["telephone2"]               = insurance.phone;
            entity.Attributes["fax"]                      = insurance.fax;
            entity.Attributes["address1_composite"]       = insurance.address1;
            entity.Attributes["address2_composite"]       = insurance.address2;
            entity.Attributes["address1_city"]            = insurance.city;
            entity.Attributes["address1_stateorprovince"] = insurance.state;
            entity.Attributes["address1_postalcode"]      = insurance.zip;
            entityRepository.CreateEntity(entity);
            return(true);
        }
        public async Task <List <Enrollment> > GetEnrollments(string practiceId)
        {
            List <Enrollment> Enrollments = new List <Enrollment>();

            QueryExpression query = new QueryExpression(Incident.EntityLogicalName);
            // query.PageInfo = new PagingInfo();
            //  query.PageInfo.Count = 200; // or 50, or whatever      // This is to limit the number of records to be fetched //
            //  query.PageInfo.PageNumber = 1;
            //query.ColumnSet = new ColumnSet("ticketnumber", "mzk_baseline");
            //query.Criteria.AddCondition("mzk_practice", ConditionOperator.Equal, new Guid(practiceId));
            FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.And);

            if (!string.IsNullOrEmpty(practiceId))
            {
                childFilter.AddCondition("mzk_practice", ConditionOperator.Equal, new Guid(practiceId));
            }
            childFilter.AddCondition("mzk_baseline", ConditionOperator.NotNull);
            childFilter.AddCondition("mzk_insuranceverified", ConditionOperator.Equal, true);
            childFilter.AddCondition("mzk_senttobilling", ConditionOperator.Equal, false);


            query.ColumnSet = new ColumnSet("ticketnumber", "mzk_baseline");

            LinkEntity Link1 = new LinkEntity(Incident.EntityLogicalName, Contact.EntityLogicalName, "customerid", "contactid", JoinOperator.Inner)
            {
                Columns     = new ColumnSet("contactid", "mzk_patientname", "firstname", "middlename", "lastname", "address1_composite", "address2_composite", "address1_city", "address1_stateorprovince", "address1_postalcode", "telephone2", "emailaddress1", "birthdate", "gendercode"),
                EntityAlias = "EnrollPatients",
            };

            LinkEntity Link2 = new LinkEntity("incident", "mzk_caseinsurancecarrier", "incidentid", "mzk_caseid", JoinOperator.LeftOuter)
            {
                Columns     = new ColumnSet("mzk_insurancetype"),
                EntityAlias = "EnrollInsurance",
            };
            LinkEntity Link3 = new LinkEntity("mzk_caseinsurancecarrier", "mzk_patientinsurancecarrier", "mzk_patientinsurancecarrier", "mzk_patientinsurancecarrierid", JoinOperator.LeftOuter)
            {
                Columns     = new ColumnSet("mzk_account", "mzk_patientinsurancepolicynumber"),
                EntityAlias = "CasePatient",
            };
            LinkEntity Link4 = new LinkEntity(Contact.EntityLogicalName, Account.EntityLogicalName, "contactid", "primarycontactid", JoinOperator.Inner)
            {
                Columns     = new ColumnSet("accountnumber"),
                EntityAlias = "ContactAccount",
            };

            Link1.LinkEntities.Add(Link4);
            Link2.LinkEntities.Add(Link3);
            query.LinkEntities.Add(Link1);
            query.LinkEntities.Add(Link2);

            OrderExpression orderby = new OrderExpression();

            orderby.AttributeName = "incidentid";
            orderby.OrderType     = OrderType.Ascending;

            query.Orders.Add(orderby);

            SoapEntityRepository entityRepository = SoapEntityRepository.GetService();
            EntityCollection     entitycollection = entityRepository.GetEntityCollection(query);
            var c = entitycollection;


            var groupedEnrollments = entitycollection.Entities.GroupBy(item => (item.GetAttributeValue <Guid>("incidentid")));

            foreach (var groupedEnrollmentsCurrent in groupedEnrollments)
            { // For each grouped enrollment
                Enrollment       model   = new Enrollment();
                Patient          patient = new Patient();
                Insurance        insurance;
                List <Insurance> InsuranceList = new List <Insurance>();
                foreach (Entity entity in groupedEnrollmentsCurrent)
                {
                    string accTypeText = "";
                    if (entity.Attributes.Contains("EnrollInsurance.mzk_insurancetype"))
                    {
                        accTypeText = entity.FormattedValues["EnrollInsurance.mzk_insurancetype"].ToString();
                    }

                    if (entity.Attributes.Contains("mzk_baseline"))
                    {
                        if (entity.Attributes.Contains("ticketnumber"))
                        {
                            model.enrollmentId = entity["ticketnumber"].ToString();
                        }
                        if (entity.Attributes.Contains("mzk_baseline"))
                        {
                            model.baselineDate = Convert.ToDateTime(entity["mzk_baseline"]).ToString();
                        }
                        if (entity.Attributes.Contains("EnrollPatients.contactid"))
                        {
                            patient.patientId = ((AliasedValue)entity["EnrollPatients.contactid"]).Value.ToString();
                        }
                        if (entity.Attributes.Contains("EnrollPatients.firstname"))
                        {
                            patient.firstName = ((AliasedValue)entity["EnrollPatients.firstname"]).Value.ToString();
                        }
                        if (entity.Attributes.Contains("EnrollPatients.middlename"))
                        {
                            patient.middleName = ((AliasedValue)entity["EnrollPatients.middlename"]).Value.ToString();
                        }
                        if (entity.Attributes.Contains("EnrollPatients.lastname"))
                        {
                            patient.lastName = ((AliasedValue)entity["EnrollPatients.lastname"]).Value.ToString();
                        }

                        if (entity.Attributes.Contains("EnrollPatients.address1_city"))
                        {
                            patient.name = ((AliasedValue)entity["EnrollPatients.address1_city"]).Value.ToString();
                        }

                        if (entity.Attributes.Contains("EnrollPatients.telephone2"))
                        {
                            patient.phone = ((AliasedValue)entity["EnrollPatients.telephone2"]).Value.ToString();
                        }
                        if (entity.Attributes.Contains("EnrollPatients.emailaddress1"))
                        {
                            patient.emailAddress = ((AliasedValue)entity["EnrollPatients.emailaddress1"]).Value.ToString();
                        }

                        if (entity.Attributes.Contains("EnrollPatients.gendercode"))
                        {
                            patient.gender = entity.FormattedValues["EnrollPatients.gendercode"].ToString();
                        }
                        if (entity.Attributes.Contains("ContactAccount.accountnumber"))
                        {
                            patient.mrn = ((AliasedValue)entity["ContactAccount.accountnumber"]).Value.ToString();
                        }
                        //((AliasedValue)entity["EnrollPatients.gendercode"]).Value.ToString();

                        insurance = new Insurance();
                        if (entity.Attributes.Contains("CasePatient.mzk_account"))
                        {
                            insurance.insuranceName = entity.FormattedValues["CasePatient.mzk_account"].ToString();//lookupname;//((AliasedValue)entity["CasePatient.mzk_account"]).Value.ToString();
                        }
                        if (entity.Attributes.Contains("CasePatient.mzk_patientinsurancepolicynumber"))
                        {
                            insurance.insurancePolicyNumber = ((AliasedValue)entity["CasePatient.mzk_patientinsurancepolicynumber"]).Value.ToString();
                        }
                        if (entity.Attributes.Contains("EnrollInsurance.mzk_insurancetype"))
                        {
                            insurance.insuranceType = accTypeText;//((OptionSetValue)((AliasedValue)entity["EnrollInsurance.mzk_insurancetype"]).Value).Value.ToString();
                        }
                        model.patient = patient;
                        if (!string.IsNullOrEmpty(insurance.insuranceName) || !string.IsNullOrEmpty(insurance.insurancePolicyNumber))
                        {
                            InsuranceList.Add(insurance);
                        }
                    }
                }
                if (InsuranceList.Count > 0)
                {
                    model.insurance = InsuranceList;
                }

                Enrollments.Add(model);
            }

            return(Enrollments);
        }