Last post Aug 11, 2016 11:42 AM by sudip_inn
Aug 10, 2016 07:18 PM|sudip_inn|LINK
Aug 11, 2016 09:20 AM|Zhi Lv - MSFT|LINK
I want to search the value in five column but I do not want hard code the fields name, rather I want do it dynamically generate where clause where all value will be search in all columns
From the above description, I suppose you want to dynamically generate where clause. If my understanding is correct, I suggest you could use PredicateBuilder to dynamically Compose Expression Predicates. You could add PredicateBuilder through the steps below:
Go to your Project -> right click Reference -> click Manager NuGet Packages... -> search and install Binbin.Linq.PredicateBuilder to your project.
Then, adding reference in your Code Behind:
Finally, you could refer to the following code to write your query statement.
using (CategoriesEnyities2 entities = new CategoriesEnyities2())
var pre = PredicateBuilder.True<Category>();
pre = pre.And(m => m.CategoryName=="Produce");
pre = pre.And(xml => xml.CategoryID == 7);
var dynamicResult = (from o in entities.Categories.Where(pre) select o).ToList();
GridView2.DataSource = dynamicResult;
The output screenshot as below:
More information about using Predicate Builder, see:
Besides, if you don't want to use this package, you can check this article:
Aug 11, 2016 11:42 AM|sudip_inn|LINK
do not understand my question properly. suppose i want to search string value in all columns of table but i do not want to hard code field name in where clause.
just where clause will be generated dynamically ?