如何融合这两个If语句以使我的Droplist过滤器协同工作以过滤数据?我有2个droplists(transfilter和Soortfilter),我希望能够从droplist 1中选择一些东西并从droplist 2中选择其他东西,然后按filter并让它显示通过过滤选择的项目两个过滤器。
@此时我返回View(query.ToList())
只显示Droplist 1中的过滤条目
Function Index(transFilter As TransactieType?, soortfilter As Soort?) As ActionResult
Dim query As IEnumerable(Of Pand)
If Not transFilter.HasValue OrElse transFilter.Value = TransactieType.Beiden Then
query = db.Panden
Else
query = db.Panden.Where(Function(p) p.TransactieType = transFilter.Value)
End If
If Not soortfilter.HasValue OrElse soortfilter.Value = Soort.All Then
query = db.Panden
Else
query = db.Panden.Where(Function(p) p.Soort = soortfilter.Value)
End If
Return View(query.ToList())
End Function
我正在尝试这个,但这并没有真正起作用
Function Index(transFilter As TransactieType?, soortfilter As Soort?) As ActionResult
Dim query As IEnumerable(Of Pand)
If Not transFilter.HasValue And soortfilter.HasValue OrElse transFilter.Value = TransactieType.Beiden And soortfilter.Value = Soort.All Then
query = db.Panden
Else
query = db.Panden.Where(Function(p) p.TransactieType = transFilter.Value And p.Soort = soortfilter.Value)
End If
Return View(query.ToList())
答案 0 :(得分:3)
问题是您在query = db.Panden
个语句的第一部分设置了If
。当您处理逐步构建LINQ查询时,始终
Dim query As IEnumerable(Of Pand)
使用query = query.Where
将每个额外的过滤器直接添加到该基本案例。
Function Index(transFilter As TransactieType?, soortfilter As Soort?) As ActionResult
Dim query As IEnumerable(Of Pand) = db.Panden
If transFilter.HasValue AndAlso transFilter.Value <> TransactieType.Beiden Then
query = query.Where(Function(p) p.TransactieType = transFilter.Value)
End If
If soortfilter.HasValue AndAlso soortfilter.Value <> Soort.All Then
query = query.Where(Function(p) p.Soort = soortfilter.Value)
End If
Return View(query.ToList())
End Function