4

In my controller, I have to test conditions based on the specified query strings that the user passed.
These are the conditions that I'm currently testing:

string dep = Request.QueryString["dep"];
string cat = Request.QueryString["cat"];
string brand = Request.QueryString["brand"];
string search = Request.QueryString["search"];

if(!string.IsNullOrEmpty(dep) && !string.IsNullOrEmpty(search))
//does the GetDepSearch() method    
}

if(!string.IsNullOrEmpty(dep) && !string.IsNullOrEmpty(brand)){
//does the GetDepBrand() method 
}

if(!string.IsNullOrEmpty(cat) && !string.IsNullOrEmpty(search)){
//does the GetCatSearch() method
}

if(!string.IsNullOrEmpty(cat) && !string.IsNullOrEmpty(brand)){
//does the GetCatBrand() method
}

if(!string.IsNullOrEmpty(dep) && !string.IsNullOrEmpty(cat) &&    
  !string.IsNullOrEmpty(search)){
//does the GetDepCatSearch() method
}

if(!string.IsNullOrEmpty(dep) && !string.IsNullOrEmpty(cat) && 
  !string.IsNullOrEmpty(brand)){
//does the GetDepCatBrand() method
}

if(!string.IsNullOrEmpty(dep) && !string.IsNullOrEmpty(cat) && 
   !string.IsNullOrEmpty(brand) && !string.IsNullOrEmpty(search)){
//does the GetDepCatBrandSearch() method
}

if(!string.IsNullOrEmpty(search) && !string.IsNullOrEmpty(brand)){
//does the GetSearchBrand() method
}

if(!string.IsNullOrEmpty(dep) && !string.IsNullOrEmpty(search) && 
  !string.IsNullOrEmpty(brand)){
//does the GetDepSearchBrand() method   
}

if(!string.IsNullOrEmpty(cat) && !string.IsNullOrEmpty(search) && 
  !string.IsNullOrEmpty(brand)){
//does the GetCatSearchBrand() method   
}

I know it is very hard to do it like that. What I want is to get the result by using any method that I query the data in my model matching the conditions based on the specified query string from the controller.
Do I have to replace that with Dynamic LinQ or anything else beside this? I really have no idea about Dynamic LinQ.

Welcome to all your answer, and thanks.

2 Answers 2

2

this problem could well be a great candidate for using predicates. i've used the alhambra predicate builder to great effect in this respect:

http://www.albahari.com/nutshell/predicatebuilder.aspx

basically, you'd create your 'null' predicate up front, and add conditions to it based on the presence of search parameters, you'd then query a single method that accepted the predicate as a parameter.

this of course assumes that your search 'object' is well specified and constant irrespective of parameter (i.e. that it's either a single 'table' or a specified set of linq joins).

hope this gives some clues.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks jim. As I see in your link, I don't know how can I call that method to use in my controller. Could you please explain me a little bit about that?
2

Assuming you query with Linq, I'd do it like this:

var query = context.CreateObjectSet<MyEntity>();
if(!string.IsNullOrEmpty(cat))
    query = query.Where(i=>i.CategoryName == cat);

if(!string.IsNullOrEmpty(brand))
    query = query.Where(i=>i.BrandName == brand);

... etc

var result = query.ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.