0

好,我的兩個表跟我實體框架5 - 使用包括

Candidates 
    Id Name 
    1 Tejas 
    2 Mackroy 

    Experiences 
    Id Designation CandidateId isDeleted 
    1 Engineer  1   true 
    2 Developer  1   false 
    3 Tester   1   true 
    4 Engineer  2   false 
    5 Tester   2   true 

模型類是應用過濾器加載相關實體:

public class Candidate 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public virtual ICollection<Experience> Experiences { get; set; } 
    } 

    public class Experience 
    { 
     public int Id { get; set; } 
     public string Designation { get; set; } 
     public int CandidateId { get; set; } 
     public bool isDeleted { get; set; } 
    } 

我希望得到得到所有的候選人以及他們的資格,但只有那些isDeleted == false

它有點像_DbContext.Candidates.Include(「Qualifications」)。ToList();

所以它會是這樣:

{1, 「Tejas的」,{2, 「開發」}},{2, 「Mackroy」,{4, 「工程師」}}

我想知道如何通過直接使用DbContext以及使用通用存儲庫來實現。

+2

可能重複http://stackoverflow.com/questions/13479352/filtering-inner-collection-with-entity-framework-5-and-repository-pattern-and-un)或http://stackoverflow.com/q/16798796/861716。 –

回答

1

創建自定義視圖模型,並與您填充它的數據:

public class CandidateViewModel { 

      CandidateViewModel() { 
       Qualifications = new List<Qualifications>(); 
      } 

      public int Id { get; set; } 
      public string Name { get; set; } 
      public List<Qualifications> Qualifications { get; set; } 
    } 

    public class Qualification { 
      public int Id { get; set; } 
      public string Label { get; set; } 
    } 
    //ViewModel 

    var result = _DbContext.Candidates.Select(o => new CandidateViewModel { 
            Id = o.Id, 
            Name = o.Name, 
            Qualifications = o.Experiences.Where(d => !d.IsDeleted).ToList() 
    }).ToList(); 
([實體框架5和存儲庫模式和工作單元內過濾收集]的
+0

謝謝@freshbm –

+1

這是懶加載它。有沒有辦法只加載未刪除的工作前? – Yashvit