使用MVC .NET中的LINQ lambda过滤带有多个文本框的数据库搜索

时间:2016-09-22 13:48:46

标签: c# asp.net linq razor lambda

我基本上创建了一个修改过的MVC模板,用于处理某些报告的CRUD操作。我想要完成的是根据多个输入过滤报告。我已经设法创建一个基本的搜索功能,基本上可以搜索数据库中的每一列(除了我还没有完全弄清楚的日期),但我想要三个文本框,每个文本框代表自己的列,用于过滤行。一个用于标识报告的列,一个用于标识日期(可能是两个,因此我可以在日期之间进行搜索),另一个用于客户。

这是控制器中的代码:

    09-22 15:41:47.056 7682-7779/com.mobile_function.webrtc_android W/System.err: com.neovisionaries.ws.client.WebSocketException: Failed to connect to '192.168.179.36:9090': failed to connect to /192.168.179.36 (port 9090): connect failed: EHOSTUNREACH (No route to host)
09-22 15:41:47.056 7682-7779/com.mobile_function.webrtc_android W/System.err:     at com.neovisionaries.ws.client.SocketConnector.doConnect(SocketConnector.java:119)
09-22 15:41:47.056 7682-7779/com.mobile_function.webrtc_android W/System.err:     at com.neovisionaries.ws.client.SocketConnector.connect(SocketConnector.java:81)
09-22 15:41:47.064 7682-7779/com.mobile_function.webrtc_android W/System.err:     at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:2022)
09-22 15:41:47.064 7682-7779/com.mobile_function.webrtc_android W/System.err:     at com.mobile_function.webrtc_android.ServerConnection$1.run(ServerConnection.java:84)
09-22 15:41:47.064 7682-7779/com.mobile_function.webrtc_android W/System.err:     at java.lang.Thread.run(Thread.java:856)
09-22 15:41:47.064 7682-7779/com.mobile_function.webrtc_android W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.168.179.36 (port 9090): connect failed: EHOSTUNREACH (No route to host)
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:114)
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err:     at java.net.Socket.connect(Socket.java:842)
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err:     at com.neovisionaries.ws.client.SocketConnector.doConnect(SocketConnector.java:110)
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err:   ... 4 more
09-22 15:41:47.071 7682-7779/com.mobile_function.webrtc_android W/System.err: Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
09-22 15:41:47.079 7682-7779/com.mobile_function.webrtc_android W/System.err:     at libcore.io.Posix.connect(Native Method)
09-22 15:41:47.079 7682-7779/com.mobile_function.webrtc_android W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-22 15:41:47.079 7682-7779/com.mobile_function.webrtc_android W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-22 15:41:47.079 7682-7779/com.mobile_function.webrtc_android W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
09-22 15:41:47.079 7682-7779/com.mobile_function.webrtc_android W/System.err:   ... 8 more

这是视图中的一些代码:

public ActionResult Avvik(string searchAWB, string searchMottaker)
    {
        var rapports = db.Rapports.Include(r => r.Årsak);

        if (!String.IsNullOrEmpty(searchAWB))
        {
            rapports = rapports.Where(r => r.AWB.Contains(searchAWB));
        }
        else if (!String.IsNullOrEmpty(searchMottaker))
        {
            rapports = rapports.Where(r => r.Mottaker.Contains(searchMottaker));
        }

        return View("Rapporter/Avvik", rapports.ToList());
    }

我在if / else中尝试了OR(||)运算符的一些变体,最后得到了上面的代码。只有第一个if语句按预期工作。

1 个答案:

答案 0 :(得分:2)

您可能想要下面的代码。 检查两个过滤器值后将检索记录。

 rapports = rapports.Where(r =>  (String.IsNullOrEmpty(searchAWB) || r.AWB.Contains(searchAWB)) 
&& (String.IsNullOrEmpty(searchMottaker) || r.Mottaker.Contains(searchMottaker)));

如果您需要满足任何一个条件的所有记录,请使用以下代码

 rapports = rapports.Where(r =>  (String.IsNullOrEmpty(searchAWB) || r.AWB.Contains(searchAWB)) 
|| (String.IsNullOrEmpty(searchMottaker) || r.Mottaker.Contains(searchMottaker)));