1

If I have an Employee entity, with derived entity types thus:

class Employee
{ // ...
}

class FireWarden : Employee
{ // ...
}

class KeyHolder : Employee
{ // ...
}

how can I query Employees that are neither fire wardens or key holders, nor any other special type of employee?

Some things I've tried:

Employees.OfType<Employee>(). ... 

returns all employees (as you'd expect).

Employees.Where(e => (e.GetType() == TypeOf(Employee)). ... 

fails with a runtime error saying something along the lines of Entity Framework doesn't support GetType().

I could write

var normalEmployees = Employees.Where(e => !(e is FireWarden || e is KeyHolder));

but then if I introduce a new entity derived from Employee, I would need to be sure to update all such queries to exclude the new type.

I suppose I could add a "SpecialEmployee" class, deriving from Employee, and derive other types form that. Then I wouldn't have to update all queries, but this feels a bit like the tail is wagging the dog.

4

1 回答 1

1

如果普通员工不能是 FireWarden 或 KeyHolder,那么您可以为 NormalEmployee 创建一个特殊类型

考虑以下

class Employee
{ // ...
}

class NormalEmployee: Employee
{
    //
}

class FireWarden : Employee
{ // ...
}

class KeyHolder : Employee
{ // ...
}

Employees.OfType<NormalEmployee>()
于 2013-06-06T14:16:24.670 回答