我工作的asp.net-MVC项目如何使用LINQ MVC
我想筛选基于参数记录使用LINQ(其中大部分是在教职员下拉列表)根据AND条件应用搜索过滤与(AND条件),但我的问题是null或空参数。
有时用户筛选记录基于一个或两个字段,其余字段值返回为空。那么就没有结果符合条件。
目前我使用(OR条件)来获取想要的记录:
public ActionResult Search(int? ReportID, int? ReportName, int? Department, string ManagerConfirmationState1, string RiskLevel, string NoteType)
{
ViewBag.ReportID = new SelectList(db.Reports, "ReportID", "ReportID");
ViewBag.ReportName = new SelectList(db.Reports, "ReportID", "ReportName");
ViewBag.Department = new SelectList(db.Departments, "DepartmentID", "DepartmentName");
ViewBag.ManagerConfirmationState1 = new SelectList(db.ManagerConfirmationState1, "ManagerConfirmationState1ID", "ManagerConfirmationState11");
ViewBag.RiskLevel = new SelectList(db.RiskLevels, "RiskLevelID", "RiskLevel1");
ViewBag.NoteType = new SelectList(db.NoteTypes, "NoteTypeID", "NoteType1");
var Notes = from n in db.Notes
select n;
//filteration
Notes = Notes.Where(n => n.ReportID == ReportID
|| n.Report.ReportID == ReportName
|| n.Report.Department.DepartmentID == Department
|| n.ManagerConfirmationState1.Equals(ManagerConfirmationState1)
|| n.RiskLevel.Equals(RiskLevel)
|| n.NoteType.Equals(NoteType));
return View(Notes.ToList());
}
查看海贼王:
@using (@Html.BeginForm("Search", "Notes", null, FormMethod.Post))
{
<div class="form-horizontal">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-2">رقم التقرير</label>
<div class="col-md-10">
@Html.DropDownList("ReportID", null, "اختـر", htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">التقرير</label>
<div class="col-md-10">
@Html.DropDownList("ReportName", null, "اختـر", htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">نوع الملاحظة</label>
<div class="col-md-10">
@Html.DropDownList("NoteType", null, "اختـر", htmlAttributes: new { @class = "form-control" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-2">الإدارة</label>
<div class="col-md-10">
@Html.DropDownList("Department", null, "اختـر", htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">اعتماد المدير</label>
<div class="col-md-10">
@Html.DropDownList("ManagerConfirmationState1", null, "اختـر", htmlAttributes: new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">درجة المخاطرة</label>
<div class="col-md-10">
@Html.DropDownList("RiskLevel", null, "اختـر", htmlAttributes: new { @class = "form-control" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="بحث" class="btn btn-default" />
</div>
</div>
</div>
}
摘要:
我可以申请过滤忽略LINQ中的空输入?
有什么建议吗?
或者,如果您不想要多个if-else,则可以使用表达式树来构建动态查询。 –