titi
titi

Reputation: 1035

Dynamic linq query due to the query string in asp.net mvc c#

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.

Upvotes: 4

Views: 1606

Answers (2)

John Landheer
John Landheer

Reputation: 4019

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();

Upvotes: 2

jim tollan
jim tollan

Reputation: 22485

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.

Upvotes: 2

Related Questions