如果项目文本为空,则跳过查询的一部分 [英] Skip part of query if item text is empty

查看:74
本文介绍了如果项目文本为空,则跳过查询的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询

            return uow.CustomerRepo
            .Get()
            .Where
            (
                c=>
                    c.Firstname.StartsWith(customerSearch.Initial) &&
                    c.Surname  == customerSearch.Surname           &&                        
                    c.Email    == customerSearch.Email             &&
                    c.Postcode == customerSearch.PostCode
            )

如果customerSearch中的内容为空,是否可以跳过查询的一部分?

Is there a way to skip parts of the query if something in customerSearch is empty?

所以我想跳过这部分

c.Surname  == customerSearch.Surname

如果

customerSearch.Surname

是空的

推荐答案

您可以使用明确检查customerSearch部分的条件来做到这一点:

You can do it with a condition that checks the customerSearch part explicitly:

.Where
(
    c=>
        (customerSearch.Initial == null || c.Firstname.StartsWith(customerSearch.Initial)) &&
        (customerSearch.Surname == null || c.Surname  == customerSearch.Surname)           &&                        
        (customerSearch.Email == null || c.Email    == customerSearch.Email)             &&
        (customerSearch.PostCode == null || c.Postcode == customerSearch.PostCode)
)

如果您需要检查空字符串而不是null,请相应地更改条件.

If you need to check for empty strings rather than null, change the condition accordingly.

这篇关于如果项目文本为空,则跳过查询的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆