I am trying to use a generic method for foreach loop which would pass different parameter as the argument.
In this example below I would like to pass different parameters(EmployeeDisplayOrder or EmployeeEnrollOrder)
public void SaveEmployeeDisplayOrder(ICollection<Employee> employees)
{
//some code
foreach( var emp in employees)
{
UpdateSpecificEmployeeOrder(employee.id, e => e.EmployeeDisplayOrder);
}
}
public void SaveEmployeeEnrollOrder(ICollection<Employee> employees)
{
//some code
foreach( var emp in employees)
{
UpdateSpecificEmployeeOrder(employee.id, e => e.EmployeeEnrollOrder);
}
}
I would like to have something like this
public void UpdateEmployeeOrders(ICollection<Employee> employee)
{
foreach( var emp in employees)
{
UpdateSpecificEmployeeOrder(employee.id, //generic property);
}
}
and call this UpdateEmployeeOrders generic method from SaveEmployeeDisplayOrder and SaveEmployeeEnrollOrder.
Method signature for UpdateSpecificEmployeeOrder
UpdateSpecificEmployeeOrder( int employeeid, params Expression<Func<Employee, object>>[] property)
Is this possible?
You have the answer in your answer... you're using lambdas... so pass a delegate in your method.
Then you would call it similar to what you had:
or