Can I make a function to be called within Linq functions using EF?
for example
int GetEmployeeStatus(string empID)
{
using (DB = new db())
{
var status = db.EmployeeStatus
.Where(e => e.EmpID == empID && e.StatusEndDate == null)
.Select(e => e.Status)
.SingleOrDefault();
return status;
}
}
Now is there a way to use the function above anywhere in my applciation in some way like this:
....
var empList = db.Employees
.Where(e => e.CostCenterID == 123
&& GetEmployeeStatus(e.EmpID) == 1);
....
I do not want to write the creteria for finding the employee status over and over again, is there a way to do that or something similar in concept?
One more thing, I know the way I write the function up will cause a database trip for every row, I hope there is way to avoid that and just to embed the query within the Linq so it will be called once.