简体   繁体   中英

“And” condition in C#/LINQ Query

partial void PrintDocLetter1_CanExecute(ref bool result)
    {
        if (this.PatientsMasterItem.DoctorsMasterItem != null)
        {

            var Doctor = PatientsMasterItem.DoctorsMasterItem;

            var PatientList = Doctor.PatientsMasterItem;

            var Letters = PatientsMasterItem.LettersSentItem;

            if ((PatientList.Count() > 1) && (Letters.Any(i => i.LetterType == "DoctorLetter1")))
            {
                result = false;
            }
            else
            {
                result = true;
            }
        }


    }

I think something is wrong with my second condition. I'm trying to find two things. 1) Doctors with more than 1 patient. 2) Among those patients whether a lettertype called "DoctorLetter1" has been sent or not.

The above code is working good for that particular record but not working other patients with same doctors where patient1 has already been sent with DoctorLetter1.

in this condition

(Letters.Any(i => i.LetterType == "DoctorLetter1")

updated (as per your ER diagram)

you didn't check all Patients 's Letter. try..

if(Doctor.PatientsMasterItem.Count > 1 
&& Doctor.PatientsMasterItem.Any(patient => 
  patient.LettersSentItem.Any(letter => letter.LetterType == "DoctorLetter1")))
{
  result = false;
}

*logic

Doctor has many patients and each patient has many letters.

if any patient (only one of them) has at least one "DoctorLetter1" sent then condition is true and result = false

Try something like this

PatientList.Count(c=>c.Letters.Any(i => i.LetterType == "DoctorLetter1")) > 1

Hope there is a relationship between Patient and Letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM