Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net ASP NET MVC VB如果searchstring为空do或else LINQ_Asp.net_Asp.net Mvc_Vb.net_Linq - Fatal编程技术网

Asp.net ASP NET MVC VB如果searchstring为空do或else LINQ

Asp.net ASP NET MVC VB如果searchstring为空do或else LINQ,asp.net,asp.net-mvc,vb.net,linq,Asp.net,Asp.net Mvc,Vb.net,Linq,现在我的控制器中有了这段代码 如果searchstring为空,我想做什么?返回所有结果,但如果它不为空,则过滤那些包含searchstring的结果。我如何才能做到这一点,这样我就不必编写多个查询来过滤数据?您可以仅在需要时添加Where子句。而不是 Function Index(SearchString As String, page As Integer?) As ActionResult If page Is Nothing Then page =

现在我的控制器中有了这段代码


如果searchstring为空,我想做什么?返回所有结果,但如果它不为空,则过滤那些包含searchstring的结果。我如何才能做到这一点,这样我就不必编写多个查询来过滤数据?

您可以仅在需要时添加
Where
子句。而不是

Function Index(SearchString As String, page As Integer?) As ActionResult
        If page Is Nothing Then
            page = 1
        Else
            page = page
        End If
        Dim List = (From m In db.maintenances Where (m.description OrElse m.description.Contains(SearchString)))

        Dim pagesize = 25
        Dim pageNumber As Integer = (If(page, 1))
        Return View(List.OrderBy(Function(o) o.id).ToPagedList(pageNumber, pagesize))
    End Function
仅当需要时,才更改为Linq扩展方法语法并添加
Where
子句:

Dim List = (From m In db.maintenances Where (m.description OrElse m.description.Contains(SearchString)))
只有在调用
ToPagedList
时,查询才会在方法末尾执行,因此只有在没有搜索子句的情况下才会检索所有记录

如果需要添加其他条件,只需添加另一个
Where
子句,例如:

Dim List = db.maintenances.AsQueryable()
If Not String.IsNullOrWhitespace(SearchString) Then
    List = List.Where(Function(x) m.description IsNot Nothing AndAlso m.description.Contains(SearchString))
End If

由于某种原因,它返回这个错误{“将varchar值'Test.'转换为数据类型bit时,转换失败。”}@JohnTrumpe我认为错误在我从您的示例中获取的部分
m.description OrElse
中。我将更新我的答案。还有一个问题,我想在过滤器中添加一个分支id,它是一个整数。我将如何使用您的示例进行此操作?有什么想法吗?为此?@JohnTrumpe:如果没有搜索词,是否也会使用分支id?基本上,您可以根据需要将其集成到
Where
子句中。
Dim List = db.maintenances.AsQueryable()
If Not String.IsNullOrWhitespace(SearchString) Then
    List = List.Where(Function(x) m.description IsNot Nothing AndAlso m.description.Contains(SearchString))
End If
If selectedBranchId IsNot Nothing Then
    List = List.Where(Function(x) m.branch_id = selectedBranchId)
End If