1

我有一个如下模型:

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; }
}

(如果它很重要,我正在使用这个基础设施)。

我需要的是让所有不是经理的员工。只有一个 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;

题外话:继承是这种情况的正确选择吗?你如何建模?我只是觉得继承导致我走错了路。

4

1 回答 1

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;
于 2011-01-29T02:09:21.917 回答