1

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.

Community
  • 1
  • 1
Cosmin Onea
  • 2,698
  • 1
  • 24
  • 27

1 Answers1

0

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;
Ramón García-Pérez
  • 1,880
  • 2
  • 20
  • 25