I'm developing an application (.Net Core 3.1, C# 8) that is using Entity Framework Core.
I would like to filter a table with several filtering options.
I'm getting the filter conditions in JSON and I'm deserializing it into an object. I want to write a where LINQ query which will filter the table based on those dynamic filtering options.
The twist is I need to manage the filtering with many options and combinations.
- You can filter for the
market,country,vendorand the rest of the filter options will benull. - You would like to filter for
countryandvendorthen themarketwill benulland also the rest of the filter options.
I'm querying a huge table, so it is important to write a query which translates fully into SQL.
The following code is not working properly. I'm looking for something similar that can solve this issue:
var filters = new demoFilterEntity()
{
Market = new List<string>() { "LAT", "NAM" }
};
var filteredData = demoMainRepository.GetAll().Where(x =>
x.Market != null && (filters.Market != null ? filters.Market.Contains(x.Market) : false) &&
x.Country != null && (filters.Country != null ? filters.Country.Contains(x.Market) : false)).ToList();
I would appreciate suggestions on how I can get through this and manage the filtering dynamically.