c# - How can i create a filter by with Linq? -


my mvc view displays following

enter image description here

i perform filtering linq based on user selection can display filtered results only.

my controller

var products = db.products.asqueryable();  public class filterpageviewmodel {     public int?[] man { get; set; }     public int?[] size { get; set; }     public int?[] color { get; set; }     public decimal? minprice { get; set; }     public decimal? maxprice { get; set; }     public sorting sorting {get ; set; }     public order order {get; set; } }  public actionresult index(int? categoryid = 0,                            int? page = 0,                           filterpageviewmodel model = null) {      //manufacturer #############################################     if (model.man != null)      {         foreach (int? in model.man)          {             if (i.hasvalue)              {                 //do here             }         }     }      //size ######################################################     if (model.size != null)     {         foreach (int? in model.size)         {             if (i.hasvalue)             {                 //do here             }         }     }      //color ######################################################     if (model.color != null)     {         foreach (int? in model.color)         {             if (i.hasvalue)             {                 //do here             }         }     } } 

i guess should perform like...

where brand 'nike' or 'salomon'  , size 'l' or 'xl' or 'xxl' , color 'red' or 'green' or 'blue' 

of course text within '' above id's , replaced clarification purposes.

i sorry if have not made self clear tongue language not english.

you can along these lines since have iqueryable provider linq sql.

var filteredproducts = products; if(model.man != null)     filteredproducts = filteredproducts.where(x => (from man in model.man man.hasvalue() select man.value).contains(x.manid)); if(model.size != null)     filteredproducts = filteredproducts.where(x => (from size in model.size size.hasvalue() select size.value).contains(x.sizeid)); //etc var enumerate = filteredproducts.tolist(); 

because you're working iqueryable, filters won't evaluated until enumerate object call tolist() or plug foreach.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -