好的我和我一起有两张桌子
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 以及使用通用存储库来实现这一点。
答案 0 :(得分: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();