I'm using Mock Data for prototyping something. I want to simulate a search on different fields. Not all search parameters must be filled. I would like to filter my Mock Data with the set filters, but I can't seem to make it work.
This is what I have: A list of modules with some properties. like ID, Name Code, etc.
var AllModules = new List<Module>{
new Module{Id=1, Name="MockName", Code="ModCode", Active:false},
new Module{Id=2, Name="MockNameFoo", Code="ModCodeFoo", Active:true}
}
Then I would like to add where clauses depending on the filters properties.
class Filter{
string Text {get;set;}
bool? IsActive {get;set;}
}
then I would like to filter the list depending if the filters are set or not, so I have tried:
public <List<Module> GetModules(Filter modulesQuery)
{
var query = from module in AllModules.AsQueryable()
select module;
if (modulesQuery.IsActive.HasValue)
{
query.Where(x => x.Active== modulesQuery.IsActive.value);
}
if (!string.IsNullOrEmpty(modulesQuery.Text))
{
query.Where(x => x.Name.Contains(modulesQuery.Text,StringComparison.CurrentCultureIgnoreCase)||
x.Code.Contains(modulesQuery.Text, StringComparison.CurrentCultureIgnoreCase)));
}
return query.ToList();
}
This is however not working and always returns me the complete list. It ignores all the where clauses, event thou I see that it goes to each if statements and "adds" them.
I don't know if this is completely wrong? I had the same thing when I first had the code with predicate Builder. BTW this is a .netCore project... but I don't think that make a difference here.
Thank you