C#C
C#8mo ago
4 replies
enrico11011

✅ Passing lambda expressions as function parameters - can it be done?

So i've got a bunch of LINQ stuff like this:

dbContext.Employees.Where(e => e.Department == "foo").ToList()


is it possible to take the lambda expression (predicate?), bind it to a var and then pass it in like so:

var predicate = e => e.Department == "foo";
dbContext.Employees.Where(predicate).ToList();


context - I'm trying to make a function that lets you filter on the employee's "Department" and/or "Role" and i'm trying to figure out if I can just pass a different expression into Where()
Was this page helpful?