我有一个查询
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
,请相应地更改条件