Reputation: 733
imagine the JSON array to have a list of key-value pairs.
<search_parameter>: <value>
...
name: "John"
age:"20"
dept: "Development"
...
Now, I want to dynamically query the employee list based on the key-value pairs I receive. For each time, I receive a different number of key-value pairs.
I may receive name alone sometimes and I may receive age as well at times. It could be just the dept at times.
Upvotes: 0
Views: 51
Reputation: 683
without knowing details I would say you can easily add where predicates on your linq query based on the available filters.
public class InputFilters
{
public string name { get; set; }
public string age { get; set; }
public string dept { get; set; }
}
let's say variable input has search parameters.
then linq would be
var result = from emp in _context.Employees
select emp;
if(!string.IsNullOrEmpty(input.name))
result = result.where(e => e.name == input.name);
if(!string.IsNullOrEmpty(input.age))
result = result.where(e => e.age == input.age);
if(!string.IsNullOrEmpty(input.dept))
result = result.where(e => e.dept == input.dept);
Upvotes: 1