Let's say we are using EF6, and db
is the EF DbContext. Let's say the user could enter five possible queries:
- Get cars where age EQUALS 10.
- Get cars where age IS LESS THAN 10.
- Get cars where age IS MORE THAN 10.
- Get cars where age is NOT EQUAL to 10.
- Get carss where age is ANY.
enum Operator { EQUAL, NOT_EQUAL, LESS_THAN, MORE_THAN, ANY }; var operator = //some user selected operator, out of the ones available in the enum above var carAge = //int selected by user to specify desired age List cars; switch(operator) { case Operator.EQUAL { cars = db.Cars.Where(c=> c.Age == carAge).ToList(); } case Operator.NOT_EQUAL { cars = db.Cars.Where(c=> c.Age != carAge).ToList(); } case Operator.LESS_THAN { cars = db.Cars.Where(c=> c.Age c.Age > carAge).ToList(); } case Operator.ANY { cars = db.Cars.ToList(); } }
How do I do this more efficiently? The switch operator seems silly. It looks like this article is answering the question, but I don't fully understand.
We do have an answer on dynamically building multiple queries, but that's a different part of what I want (would apply if question was "User can search for both car age and mileage, or neither).
Edit: To clarify in response to a comment, my "goal" is any standard search, using the car example, a user would have a number of fields where they can select operators. They can use any, all, or no fields, e.g.
Mileage: Operator, Value
Year: Operator, Value
Brand: (is/is not, for example), Car brands