如果条目文本为空,则跳过部分查询



我有一个查询

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

是否有办法跳过查询的部分,如果在customerSearch的东西是空的?

所以我想跳过 这部分
c.Surname  == customerSearch.Surname
如果

customerSearch.Surname

您可以通过显式检查customerSearch部分的条件来做到这一点:

.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,请相应地更改条件

相关内容

  • 没有找到相关文章

最新更新