I have a Linq expression that operates on a list of objects for which one of said objects properties I need to type check before making use of it.
Example:
IEnumerable<Employee> activeAuditOwners = (
from objectStateEntry in objectStateEntries
where ( objectStateEntry.Entity is IAuditEntity ) == true
&& ( objectStateEntry.Entity as IAuditEntity ).Active == true
select ( objectStateEntry.Entity as IAuditEntity ).Owner
);
My concern is that I use type checking 3 times ( is, as, as ) which doesn’t feel very DRY.
Is there a better shape for this query that avoids this (without creating a second query)?
UPDATE: Thanks for the great answers, I have tidied the example a bit for future readers.
One way would be to use the
letclause: