1

我有類似下面的模式:EF4 CTP5多態查詢

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

也許你會發現,一個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;