我有一个如下模型:
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; }
}
(如果重要,我正在使用this infrastructure。)
我需要的是让所有非经理的员工。只有一个OfType扩展方法。我需要的是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;
偏离主题:继承是这种情况的正确选择吗?你是如何建模的?我只是觉得继承导致我走错了路。
答案 0 :(得分:0)
也许您会发现NotOfType扩展方法应该更好,但这应该适合您。
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;
或者像这样:
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;