user3733078
user3733078

Reputation: 249

Confused about Linq query

I have created a simple db named Hospital and I have a few columns enter image description here.I have filled first dropdown named drdoctors.And it works

 protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack) {

            fetchDoctors();
        }

    }

    void fetchDoctors() {

        HospitalEntities entitiy = new HospitalEntities();
        List<Doctor> doc = entitiy.Doctors.ToList();
        drDoctor.DataSource = doc;
        drDoctor.DataTextField = "Name";
        drDoctor.DataValueField = "DoctorNo";
        drDoctor.DataBind();


    }

What I want to do is fill the other dropdown with the this doctor's patients .

 protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
    {
       int id= Int32.Parse(  drDoctor.SelectedValue);
       HospitalEntities entities = new HospitalEntities();

        var query= from p in entities.Doctors 


    }

But linq queries are so complicated.How can i do this

Upvotes: 0

Views: 42

Answers (2)

DLeh
DLeh

Reputation: 24395

Looking at your diagram, it looks you don't have foreign key relationships set up. I would highly recommend doing this (for uncountable reasons). But by doing this, you will be able to "join" on tables much more easily like this:

protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
   int id = Int32.Parse(drDoctor.SelectedValue);
   HospitalEntities entities = new HospitalEntities();


    drDoctor.DataSource = entities.Doctors
        .Where(x => x.DoctorNo == id)
        .SelectMany(x => s.MedExams.Select(y => y.Patients));
    drDoctor.DataBind();
}

Upvotes: 0

This should about do it. Please note, this code wasn't tested and may contain minor errors.

 protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
    {
       int id= Int32.Parse(  drDoctor.SelectedValue);
       HospitalEntities entities = new HospitalEntities();

        var query= (from d in entities.Doctors 
                    join m in entities.MedExams on d.DoctorNo equals p.DoctorNo
                    join p in entities.Patients on m.PatientNo equals p.PatientNo
                    where d.DoctorNo == id
                    select p).ToList();

        //Populate Patients from query

    }

Upvotes: 1

Related Questions