I am using search functionality in this i have threee fileds
我正在使用搜索功能,我有三个文件
Example:- Name, State, age
示例: - 姓名,州,年龄
when i select the name and search it should display the related record,
当我选择名称并搜索时,它应显示相关记录,
and when i select Name, State and search i should display the record which the both selected options are included in the record,
当我选择名称,状态和搜索时,我应该显示记录中包含两个选定选项的记录,
if the record is not having any of the selected option then it should not display the record,
如果记录没有任何选定的选项,那么它不应该显示记录,
so for this i should use dynamic query using lambda expression
所以为此我应该使用lambda表达式进行动态查询
so please send me any example to this.
所以请把这个例子发给我。
1 个解决方案
#1
1
You can use like this , Make use of Predicate Builder
您可以像这样使用,使用Predicate Builder
I have same application you can check here : Dynamic query with Linq
我有相同的应用程序,您可以在这里查看:使用Linq进行动态查询
var predicate = PredicateBuilder.True<employee>();
if(!string.IsNullOrEmpty(txtAddress.Text))
predicate = predicate.And(e1 => e1.Address.Contains(txtAddress.Text));
if (!string.IsNullOrEmpty(txtEmpId.Text))
predicate = predicate.And(e1 => e1.Id == Convert.ToInt32(txtEmpId.Text));
if (!string.IsNullOrEmpty(txtDesc.Text))
predicate = predicate.And(e1 => e1.Desc.Contains(txtDesc.Text));
if (!string.IsNullOrEmpty(txtName.Text))
predicate = predicate.And(e1 => e1.Name.Contains(txtName.Text));
EmployeeDataContext edb= new EmployeeDataContext();
var emp = edb.Employees.Where(predicate);
grdEmployee.DataSource = emp.ToList();
grdEmployee.DataBind();
Dynamically Composing Expression Predicates
动态编写表达式谓词
#1
1
You can use like this , Make use of Predicate Builder
您可以像这样使用,使用Predicate Builder
I have same application you can check here : Dynamic query with Linq
我有相同的应用程序,您可以在这里查看:使用Linq进行动态查询
var predicate = PredicateBuilder.True<employee>();
if(!string.IsNullOrEmpty(txtAddress.Text))
predicate = predicate.And(e1 => e1.Address.Contains(txtAddress.Text));
if (!string.IsNullOrEmpty(txtEmpId.Text))
predicate = predicate.And(e1 => e1.Id == Convert.ToInt32(txtEmpId.Text));
if (!string.IsNullOrEmpty(txtDesc.Text))
predicate = predicate.And(e1 => e1.Desc.Contains(txtDesc.Text));
if (!string.IsNullOrEmpty(txtName.Text))
predicate = predicate.And(e1 => e1.Name.Contains(txtName.Text));
EmployeeDataContext edb= new EmployeeDataContext();
var emp = edb.Employees.Where(predicate);
grdEmployee.DataSource = emp.ToList();
grdEmployee.DataBind();
Dynamically Composing Expression Predicates
动态编写表达式谓词