【问题标题】:How to apply search filtration based on AND condition using LINQ MVC如何使用 LINQ MVC 基于 AND 条件应用搜索过滤
【发布时间】: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


    【解决方案1】:

    只需逐步构建查询:

    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 =&gt; cond1(y) &amp;&amp; cond2(y))在功能上等同于x.Where(y =&gt; cond1(y)).Where(y =&gt; cond2(y))

    【讨论】:

    • 或者,如果您不想要多个 if-else,您可以使用表达式树来构建动态查询。
    【解决方案2】:

    如果您要过滤的参数数量有限(甚至一个可能为 null 或空的参数),您可以在使用 AND 时在查询本身中检查其值。

    如果未提供过滤器值,则您希望选择所有(当前过滤的)记录而不进行进一步过滤,否则您希望将过滤器值添加到当前过滤的记录中。

    我将使用空检查,因为您已显示参数可以为空。

    Notes = Notes.Where(n => 
    (ReportId == null || n.ReportID == ReportID) 
    && (ReportName == null || n.Report.ReportName == ReportName) 
    && (Department == null || n.Report.Department.DepartmentID == Department) 
    && (ManagerConfirmationState1 == null || n.ManagerConfirmationState1.Equals(ManagerConfirmationState1)) 
    && (RiskLevel == null || n.RiskLevel.Equals(RiskLevel)) 
    && (NoteType == null || n.NoteType.Equals(NoteType))
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 2018-06-12
      • 1970-01-01
      相关资源
      最近更新 更多