修改实体框架查询以在多个级别中选择特定字段

时间:2018-07-18 22:46:11

标签: c# entity-framework

我的代码优先模型中有三个模型类。

public class Project
{
    public Guid Id { get; set; }
    public guid ProjectTypeId{ get; set; }
    public string projectType{ get; set; }
    [Required]
    public DateTime CreationDate{ get; set; }
    //more properties here
    //...
    public bool Activated{ get; set; }
    public IList<ProjectDetail> ProjectDetails{ get; set; }
}

您会看到一个项目有很多细节。

public class ProjectDetail
{
    [Required]
    public Guid Id { get; set; }
    public Project Project{ get; set; }
    [Required]
    public Guid ProjectId{ get; set; }             
    public string DetailDescription{ get; set; }
    public IList<ProjectDetailsAnswer> ProjectDetailsAnswers{ get; set; }
}

一个projectDetail有很多答案:

public class ProjectDetailsAnswer
{
    [Required]
    public Guid Id { get; set; }
    [Required]
    public Guid ProjectDetailId{ get; set; }
    public ProjectDetail ProjectDetail{ get; set; }       
    public string OtherField{ get; set; }
    [Required]
    [MaxLength(512)]
    public string AnswerDescription{ get; set; }
}

这是我到目前为止的查询:

var result = _dbContext.Project//<==search from details
    .Include(x => x.ProjectDetail)
    .Include(y => y.ProjectDetail.Select( c=> c.ProjectDetailAnswer))
    }).ToList();

以下是我的问题>

  1. 如何修改实体框架查询以带来
  

Project.Id,包含所有ProjectDetailsAnswer.AnswerDescription的projectDetail.DetailDescription列表。

  1. 关于视图模型的建议,其中可以包含问题1中查询的结果。

2 个答案:

答案 0 :(得分:0)

您可以使用ViewModel,例如:

empSelect.selectedIndex = 0;

您的查询将如下所示:

public class ProjectViewModel
{
    public Guid Id { get; set; }

    // this could be a Dictionary<string, List<string>> 
    // if there are no duplicate DetailDescription's for a Project 
    public List<KeyValuePair<string, List<string>>> Details { get; set; }
}

答案 1 :(得分:0)

您可以创建视图模型的层次结构:

public class ProjectViewModel
{
    public Guid Id { get; set; }

    private List<ProjectDetailViewModel> _details = new List<ProjectDetailViewModel>();    
    public List<ProjectDetailViewModel> Details 
    { 
        get {return _details; }
        set {_details = value; } 
    }
}

public class ProjectDetailViewModel
{
    public Guid Id {get; set;}
    public string Description {get; set;}

private List<AnswerViewModel> _answers = new List<AnswerViewModel>();    
    public List<AnswerViewModel> Answers 
    { 
        get {return _answers; }
        set {_answers = value; } 
    }
}

public class AnswerViewModel
{
    public Guid Id {get; set;}
    public string Description {get; set;}
}

然后

List<ProjectViewModel> result = _dbContext.Projects
    .Where(x => /* conditions */)
    .Select(x => new ProjectViewModel
    {
        Id = x.Id,
        Details = x.ProjectDetails.Select(d => new ProjectDetailViewModel
        {
            Id = d.Id,
            Description = d.Description,
            Answers = d.ProjectDetailAnswers.Select(a => new AnswerViewModel
            { 
                Id = a.Id,
                Description = a.Description
            }
        }
    }).ToList();

这将构成对数据库的查询,该查询仅返回所选字段,并为项目提供详细列表以及相关答案。

如果要使其扁平化,只需在项目下有一个答案列表(答案中包括详细说明。)

public class ProjectViewModel
{
    public Guid Id { get; set; }

    private List<ProjectDetailAnswertViewModel> _answers = new List<ProjectDetailAnswertViewModel>();    
    public List<ProjectDetailAnswertViewModel> Answers
    { 
        get {return _answers; }
        set {_answers = value; } 
    }
}

public class ProjectDetailAnswertViewModel
{
    public Guid Id {get; set;}
    public string DetailDescription {get; set;}
    public string Description {get; set;}
}

然后

List<ProjectViewModel> result = _dbContext.Project
    .Where(x => /* conditions */)
    .Select(x => new ProjectViewModel
    {
        Id = x.Id,
        Answers = x.ProjectDetails.SelectMany(d => d.ProjectDetailAnswers).Select(a => new ProjectDetailAnswerViewModel
        {
            Id = a.Id,
            DetailDescription = a.ProjectDetail.Description,
            Description = a.Description,
        }
    }).ToList();