I'm using a custom interception behaviour to filter records (filter is based upon who the current user is) however I'm having some difficulty (this is the body of the interceptors Invoke method)
var companies = methodReturn.ReturnValue as IEnumerable<ICompanyId>;
List<string> filter = CompaniesVisibleToUser();
methodReturn.ReturnValue = companies.Where(company =>
filter.Contains(company.CompanyId)).ToList();
The CompaniesVisibleToUser provides a string list of company id's that the user is allowed to view.
My problem is the incoming data - companies - will be a IList of various types all of which should implement ICompanyId in order for the data to be filtered on companyId. However, it seems that the cast - as IEnumerable results in the data being returned as this type which causes problems further up the call stack.
How can I perform a filter without changing the return type?
The exception I get is
Unable to cast object of type 'System.Collections.Generic.List1[PTSM.Application.Dtos.ICompanyId]' to type 'System.Collections.Generic.IList
1[PTSM.Application.Dtos.EmployeeOverviewDto]'.
The higher caller is
public IList<ApplicationLayerDtos.EmployeeOverviewDto> GetEmployeesOverview() { return _appraisalService.GetEmployeesOverview(); }
If I change the
IEnumerable <ICompanyId>
to IEnumerable<EmployeeOverviewDto>
it works as expected but obviously this is not what I want as the list being filtered will not always be of that type.