Need help using PredicateBuilder

I need to know about using PredicateBuilder . In almost every code usage example, they display the code as follows:

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

What is an Employee object that is between the brackets larger and smaller? I put brains on him. I use Linq for SQL entities, and I get compilation errors when I do this myself. I think errors are something like:

"Cannot translate from Linq table to ..."

I'm new at this. Please forgive me for asking what might be obvious. Thanks.

+6
source share
1 answer

Since @MatsRietdijk specified in the comments section is used to generate. Basically, using generics you can create a method that will work with a type / class ("employee" in this case) that is unknown (although you can control this with where ).

If you want to change the employee type to another type (for example, customer ), then the properties available in lambda expressions will also differ based on any properties published publicly. For instance:

 var predicate = PredicateBuilder.True<customer>(); // There is no "CustomerName" property from employee, but there is for customer objects if (!string.IsNullOrEmpty(txtName.Text)) predicate = predicate.And(e1 => e1.CustomerName.Contains(txtName.Text)); 
+2
source

All Articles