在 MVC 中使用多个下拉列表进行过滤

Filtering with Multiple DropDown Lists in MVC

我正在从事 3 层架构项目 我有多个下拉列表来过滤使用 LINQ 从数据库中获取的数据。

现在我需要一种方法来过滤这些下拉菜单,当我 select 任何下拉菜单中的一个项目时它正在过滤,当我 select 从两个下拉菜单中过滤这两个 select离子等等...

我这样使用 linq:

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;

而且只有当我 select 所有下拉菜单时才过滤,而不是单独过滤。

请帮忙:)

您将必须执行有条件的 where 子句,例如

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();