Reputation: 305
I have apartment entity and I want to get apartments based on my filters.
this is my search entity
public class Search
{
public bool isStudio {get;set;}
public bool isNoPlanning {get;set;}
public bool isMultiRoom {get;set;}
public int[] NumberOfRooms {get;set;}
}
this is my current search logic.
var apartments = buildRepost.Get(buildId).Where(condStates =>
(searchModel.NumberOfRooms != null
&& searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString())
|| ((searchModel.IsStudio && condStates.IsStudio))
|| ((searchModel.IsNoPlanning && condStates.IsFreePlaning))
|| ((searchModel.IsMultiRoom && condStates.RoomsCount >= 4)));
The problem with this logic is that I got the wrong result when all fields are false and null
. For example when isStudio
, IsNoplaning
and isMultiRoom
are false and numberofRooms
is null
I should have got all apartments but instead, I got an empty array. Any help?
Upvotes: 0
Views: 116
Reputation: 1803
the searchModel.NumberOfRooms != null
checker in the where clause causes the problem, you are not mathcing it with any of the condStates
properties and the searchModel.Is.....
properties
Make your searchModel
as a checker in an if
statement then build the query from the if searchModel
conditions.
var query = buildRepost.Get(buildId).AsQueryable();
if (searchModel.NumberOfRooms != null)
{
query = query.Where(condStates => searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString());
}
if (searchModel.IsStudio)
{
query = query.Where(condStates => condStates.IsStudio);
}
if (searchModel.IsNoPlaning)
{
query = query.Where(condStates => condStates.IsFreePlaning)
}
if (searchModel.IsMultiRoom)
{
query = query.Where(condStates => condStates.RoomsCount >= 4)
}
var results = query.ToList()
Upvotes: 2
Reputation: 11841
(searchModel.NumberOfRooms != null
&& searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString())
This is always going to be false when the NumberOfRooms
is null
(due to the null check), and given that the other bool values are all false, you will get no results.
Instead if you change to:
(searchModel.NumberOfRooms == null
|| searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString())
You will either get everything (when the NumberOfRooms
is null, or just the records that match the RoomsCount
(when the NumberOfRooms
is not null).
Note that in this case, if NumberOfRooms
is null you will still return everything regardless of your bool filters. Which seems to be what your code requires, but I'm not sure is what you actually require, so you might want to check that.
Upvotes: 0