EF4 CTP5 polymorphic query
I have a model like the following:
public class Employee
{
public Employee()
{
TimeCards = new List<TimeCard>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; }
public virtual ICollection<TimeCard> TimeCards { get; set; }
}
public class Manager : Employee
{
public bool HasCompanyCar { get; set; }
}
public class Developer : Employee
{
public string MainProgrammingLanguage { get; set; }
}
(I am using this infrastructure if it is important).
What I need is to get all the employees that are not managers. There is only an OfType extension method. What I need is NotOfType.
var employees = unitOfWork.Employees
.FindAll()
.NotOfType<Manager>(); //or something similar
var cards = unitOfWork.TimeCards.FindAll();
开发者_如何学编程
var query = from e in employees
from tc in cards
where tc.Employee.Id == e.Id && e.Name.StartsWith("C")
select tc;
Off-topic: is inheritance the right choice for this kind of situations? How do you model it? I just feel that inheritance leads me down the wrong path.
Maybe you would find out that a NotOfType extension method should be better, but this should work for you.
var employees = unitOfWork.Employees
.Where(e => !(e is Manager));
var cards = unitOfWork.TimeCards.FindAll();
var query = from e in employees
from tc in cards
where
tc.Employee.Id == e.Id &&
e.Name.StartsWith("C")
select tc;
Or like this:
var employees = unitOfWork.Employees
.FindAll();
var cards = unitOfWork.TimeCards.FindAll();
var query = from e in employees
from tc in cards
where
tc.Employee.Id == e.Id &&
!(e is Manager) &&
e.Name.StartsWith("C")
select tc;
精彩评论