【发布时间】:2017-07-16 21:57:48
【问题描述】:
我正在做 asp.net-mvc 项目
我想使用带有(AND 条件)的 LINQ 根据参数(其中大部分是 Veiw 中的下拉列表)过滤记录,但我的问题是 null 或空参数。
有时,用户会根据一个或两个字段过滤记录,而其余字段值返回为空。那么就没有满足条件的结果。
目前我使用(或条件)来获取想要的记录:
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 中应用过滤并忽略空输入吗?
有什么建议吗?
【问题讨论】:
标签: c# asp.net-mvc linq search filtering