2017-02-26 60 views
1

我工作的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中的空输入?

有什么建议吗?

回答

6

只是构建查询了增量:

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))

+0

或者,如果您不想要多个if-else,则可以使用表达式树来构建动态查询。 –

0

如果你有参数有限过滤(或甚至一个单一的一个可能。如果没有提供过滤器值,那么你想选择所有(当前过滤)的记录,而不需要进一步过滤,o无论您想要将过滤器值添加到当前过滤的记录中。

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

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)) 
);