2012-12-14 48 views
0

我的模型 - 員工Linq查詢階段

public class Employee 
{ 
    public int EmployeeId { get; set; } 
    public string GivenName { get; set; } 
    public string Surname { get; set; } 
    public virtual ICollection<EmployeeAudit> EmployeeAudits { get; set; } 

} 

和員工審計

public class EmployeeAudit 
{ 
    public int EmployeeAuditId { get; set; } 

    public DateTime DateCreated { get; set; } 
    public string AuditCode { get; set; } 

    public int EmployeeId { get; set; } 
    public Employee Employee { get; set; } 
} 

,所以我將有將有許多EmployeeAudits因爲這樣的員工 -

員工
id -----名稱
1 ------鮑勃
2 ------吉姆
3 ------希拉

審計
EMPID --- auditcode ----- ------------ datecreated
1 --------- created --------------------- 13/12/12
2 --------- created --------------------- 13/12/12
3 --- ------ created --------------------- 13/12/12
2 ----- ---- SubmittedToHR ----------- 13/12/12
3 --------- SubmittedToHR ----------- 13/12/12
3 --------- PassedForVerification --- 13/12/12


這些審計表示在過程的某一階段,如何查詢使用LINQ到在員工所在的過程中顯示 。

例如,我想顯示在SubmittedToHr但已創建,而不是PassedForVerification的所有員工。

+1

那麼,什麼是你的問題嗎? – ryadavilli

+0

你有什麼嘗試? –

回答

0

這個查詢將返回所有員工,其中有去年審計碼等於SubmittedToHR(假設審計碼只能從創建> SubmittedToHR> PassedForVerification改變):

var query = from e in context.Employees 
      join ea in context.EmployeeAudit 
       on e.EmployeeId equals ea.EmployeeId into g 
      where g.OrderByDescending(x => x.DateCreated) 
        .FirstOrDefault().AuditCode == "SubmittedToHR" 
      select e; 
+0

感謝Lazyberezovsky的回覆。 – Tappies

+0

我會試試這個,讓你知道我是如何得到的。我對整個.net/mvc/c#/ linq場景很陌生,所以仍然試圖找到我的腳。 – Tappies

+0

@ user1903548沒問題。這是你需要的嗎?如果審計代碼分配不連續,則應更改查詢以設置附加條件。 –