使用EF,我想查詢一個整數,但如果值爲0,我不想做查詢。查詢值,如果它不是零使用實體框架
rentalContext.Apartments
.Include(a => a.ApartmentType)
.Include(a => a.Files)
.Include(a => a.Address)
.Where(a => some other search conditions &&
(filters.CityId != 0 && a.Address.CityObject.Id == filters.CityId))
.ToList()
.OrderByDescending(a => a.Created);
它工作正常,如果CityId
不爲0,但如果是零,則返回列表中的0對象。這不是你應該這樣做的方式嗎?
'filters.CityId!= 0 &&'should be'filters.CityId == 0 || ([德摩根法]](https://en.wikipedia.org/wiki/De_Morgan%27s_laws)) –