ask*_*skm 1 c# linq asp.net-mvc search filtering
我正在研究asp.net-mvc项目
我想基于参数过滤记录(大多数是Veiw中的下拉列表)使用LINQ with(AND condition),但我的问题是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());
}
Run Code Online (Sandbox Code Playgroud)
一块视图:
@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>
}Run Code Online (Sandbox Code Playgroud)
摘要:
我可以在LINQ中使用忽略空输入来应用过滤吗?
有什么建议吗?
只需逐步构建查询:
if (field1.HasValue) {
query = query.Where(x => x.Val1 = field1.Value);
}
if (field2.HasValue) {
query = query.Where(x => x.Val2 = field2.Value);
}
Run Code Online (Sandbox Code Playgroud)
(因为x.Where(y => cond1(y) && cond2(y))在功能上相当于x.Where(y => cond1(y)).Where(y => cond2(y)).
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |