gpt4 book ai didi

c# - 如何使用 LINQ MVC 基于 AND 条件应用搜索过滤

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:05 25 4
gpt4 key购买 nike

我正在做 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());
}

一 block View :

@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 (field1.HasValue) {
query = query.Where(x => x.Val1 = field1.Value);
}
if (field2.HasValue) {
query = query.Where(x => x.Val2 = field2.Value);
}

(因为 x.Where(y => cond1(y) && cond2(y)) 在功能上等同于 x.Where(y => cond1(y)).Where (y => cond2(y)).

关于c# - 如何使用 LINQ MVC 基于 AND 条件应用搜索过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com