0

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.

2
  • What C# data types are involved? How are you handling the JSON in C#? What do you mean by dynamically? Commented Apr 30, 2018 at 18:43
  • What is the question here? Commented Apr 30, 2018 at 19:15

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.