Ibrahim Samara
Ibrahim Samara

Reputation: 111

Filtering with Multiple DropDown Lists in MVC

I am working on 3-tier Architecture project and I have multiple DropDown Lists to make filtering of data that is fetched from database using LINQ.

Now I need a way to filter with these dropdowns where when I select an item in any dropdown it is filtering and when I select from two dropdowns to filter by these two selections and so on...

I use linq like this:

var doctors = from d in db.Doctors
              where d.CityID == id
              && d.HasSecretary == hasSec
              && d.OldSystem == oldSys
              && d.MetDoctor == metDoc
              && d.PriceProblem == priceProb
              && d.EasyToConvince == easyToCon
              && d.ComputerSavvy == comSav
              && d.Sold == sold
              && d.NotInterested == notIntr
              && d.FollowUp == followUp
              && d.NewClinic == newClin
              && d.RequestedADemo == reqDemo
              select d;

And it is filtering only when I select all dropdowns, and not individually.

Please help :)

Upvotes: 1

Views: 1035

Answers (1)

CrnaStena
CrnaStena

Reputation: 3157

You will have to do conditional where clauses e.g.

var doctors = from d in db.Doctors;

if (id != null)
  doctors = doctors.Where(d => d.CityID == id);
if (hasSec)
  doctors = doctors.Where(d => d.HasSecretary == hasSec);

// other if statements

// results
var results = doctors.ToList();

Upvotes: 1

Related Questions