0

好的,我有两张桌子

    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 , "光辉" , { 2, "开发者" } }, { 2, "麦克罗伊", { 4, "工程师" } }

我想知道如何通过直接使用DbContext以及使用Generic Repository来实现这一点。

4

1 回答 1

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();
于 2013-10-22T12:57:00.947 回答