Currently, I have:
public static IList getFilteredEvents( DateTime Start, DateTime End, string env, string req) {
string QueryString;
if( env == "all" ) {
QueryString = "SELECT * FROM table WHERE date BETWEEN '" + Start + "' AND '" + END + "'";
} else {
QueryString = "SELECT * FROM table WHERE date BETWEEN '" + Start + "' AND '" + END + "' AND env='" + env + "'";
}
}
I am getting the request string by doing passing a data object from the view. In my controller, I get it like this:
string env = Request.QueryString["filter"];
Is there a better way to filter my results instead of having to write multiple queries in an if-elseif chain? How would I achieve this for multiple filters e.g. in the query AND req = '" + req + "';?
Thanks for your help!
You can append
ANDs inif-else. For example,And it’s better to use StringBuilder to build a dynamic string;