I have a collection of type objects which I know they are type Employee. I’d like to perform some linq operations on them using the employee some thing similar to this:
var options = (from e in m_Employees
select (e as Employee).DepartmentCode).Distinct();
But the as employee is not surprisingly giving an error. is there a way around it?
Changing the collection is not really an options since I,m maintaining the code and I want to avoid big changes.
You can use either
or
Castthows an error if you can not cast each item toEmployee, butOfTypewill filter out those objects not matching the type.