我使用MVC 4并且在我的视图中有5个下拉列表。当下拉列表选中文本并单击按钮搜索时,我想从4表中选择下拉列表进行选择。如果未选中下拉列表,则不会选中状态。
(from met in db.tblMet
from useMet in db.tblUseMet.Where(m => m.UseMetID == met.UseMetID_FK).DefaultIfEmpty()
from typeMet in db.tblTypeMet.Where(m => m.TypeMetID == met.TypeMetID_FK).DefaultIfEmpty()
from mod in db.tblMod.Where(m => m.ModID == met.ModID_FK).DefaultIfEmpty()
from affair in db.tblAffairs.Where(m => m.AffairID == met.AffairID_FK).DefaultIfEmpty()
from desert in db.tblDeserts.Where(m => m.DesertID == met.DesertID_FK).DefaultIfEmpty()
from city in db.tblCities.Where(m => m.CityID == met.CityID_FK).DefaultIfEmpty()
from user in db.tblUsers.Where(m => m.UserID == met.UserIDCreate_FK).DefaultIfEmpty()
from userCh in db.tblUsers.Where(m => m.UserID == met.UserIDChange_FK).DefaultIfEmpty()
from setting in db.tblSettings.Where(m => m.SettingID == met.SettingID_FK).DefaultIfEmpty()
from sim in db.tblSims.Where(m => m.SimID == mod.SimID_FK).DefaultIfEmpty()
from typemod in db.tblTypeMod.Where(m => m.TypeModID == sim.TypeModID_FK_Kind).DefaultIfEmpty()
from gro in db.tblGroupMet.Where(m => m.GroupMetID == met.GroupMetID_FK).DefaultIfEmpty()
from group1 in db.tblMetRelateGroups.Where(x => x.MetID_FK == met.MetID).DefaultIfEmpty()
from status in db.tblStatus1.Where(m => m.StatusID == met.StatusID_FK).DefaultIfEmpty()
where ((city.CityID == City1||city.CityID !=null)
&& (typeMet.TypeMetID == Type1 || typeMet.TypeMetID != null)
&& (useMet.UseMetID == Usemeter1|| useMet.UseMetID != null)
&& (group1.GroupMetID_FK ==Group1 || group1.GroupMetID_FK != null)
&& (affair.AffairID ==Affair1 || affair.AffairID != null)
//|| desert.DesertID==Desert1
)
我想选择下拉列表,如果不选择任何,则不选中查询。在上面的代码中选择零记录。
答案 0 :(得分:1)
您只需使用流畅的语法来实现此目的。这是一个例子:
var lQuery = from met in db.tblMet;
if (City1 != null)
lQuery = lQuery.Where(r => r.CityID == City1);
...
这样您就可以动态地为查询添加条件。
如果要添加具有逻辑OR的条件,则需要谓词构建器,请参阅https://stackoverflow.com/a/1775057/3936440。