If you wanted to pass in three seperate parameters using a LINQ query, you could use something like the following
(for example purposes):
//The values for each of your drop downs
string p1 = Dd1.Value;
string p2 = Dd2.Value;
string p3 = Dd2.Value;
//Your query passing in three parameters
var query = yourDataContext.Products.Where(p => p.Type == p1 && p.Feature == p2 && p.Category == p3);
If you wanted to check the values and apply filters as necessary, you could use the following method :
//The values for each of your drop downs
string type = Dd1.Value;
string feature = Dd2.Value;
string category = Dd2.Value;
//Gets all your products
var query = yourDataContext.Products;
//Checks if your first parameter is empty, if so applies a filter
query = (string.IsNullOrEmpty(type)) ? query : query.Where(p => p.Type == type);
//Checks your second parameter and filters according
query = (string.IsNullOrEmpty(feature)) ? query : query.Where(p => p.Feature == feature);
//Third Parameter
query = (string.IsNullOrEmpty(category)) ? query : query.Where(p => p.Category == category);
Both of these are shown to demonstrate how you can use filtering within LINQ in an easy fashion, as there are many, many ways to accomplish this.
krishnada25
Member
631 Points
906 Posts
Passing Parameters to a linq query
Feb 04, 2013 05:43 PM|LINK
Hi , I have a form with 3 dropdowns and when user selects those values and hits the submit button I am loading the query and populating a datagrid.
I have dropdowns with Select all as default (Applies no filter),
==Dd1== ==Dd2== ==Dd3====
Select all Select all Select All
Washers Energy Efficient Cat1
Dryers Front load Cat2
I am joining these 3 tables and returning the results. But when they pass 3 parameters how should I handle it??
Do i Have to write 3 queries?? or is there any better way to handle this??
I guess I am looking for something like pass parameters and if parameter are empty return everything.
Thanks,
Kris
Rion William...
All-Star
27148 Points
4504 Posts
Re: Passing Parameters to a linq query
Feb 04, 2013 06:48 PM|LINK
If you wanted to pass in three seperate parameters using a LINQ query, you could use something like the following (for example purposes):
If you wanted to check the values and apply filters as necessary, you could use the following method :
Both of these are shown to demonstrate how you can use filtering within LINQ in an easy fashion, as there are many, many ways to accomplish this.