代码之家  ›  专栏  ›  技术社区  ›  ev vk

实体框架代码首先在视图模型中选择多个级别

  •  -1
  • ev vk  · 技术社区  · 6 年前

    很好的一天,

    我正在使用ASPNET MVC,带C#和实体框架我的codefirst模型中有三个表。

    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; }
    }
    

    我想做的是使用带有lambda表达式的实体框架来加载viewmodel

    public class MyViewModel
    {
        public Guid projectTypeId{ get; set; }
        public string projectType { get; set; }
        public string DetailDescription { get; set; }
        public IEnumerable<string> AnswerDescription{ get; set; }//this propertie comes from ProjectDetailsAnswer.AnswerDescription
    }
    

    到目前为止我得到的是这个代码:

    var y = _dbContext.Project
        .Include(a => a.ProjectDetail.Select(p => p.ProjectDetailsAnswer))
        .Select(n =>new MyViewModel{projectTypeId=n.projectTypeId,projectType =n.Project.projectType,DetailDescription =n.AnswerDescription.Select(b =>b.AnswerDescription).ToList()})
        .ToList();
    

    我收到此错误:>无法将此代码中的字符串转换为列表。 n.AnswerDescription.Select(b =>b.AnswerDescription).ToList()

    我做错什么了?如果可能的话,你能提供一些代码来加载我的viewmodel和数据吗?

    ,问题来了

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   Dongdong    6 年前

    如果DetailDescription只是viewmodel中的一个字符串,而不是IList,那么我猜它是一个ProjectDetailViewModel,所以您的linq应该是:

            var result = _dbContext.ProjectDetails //<==search from details
                .Select(x => new ProjectDetailViewModel
                {
                    projectTypeId = x.Project.ProjectTypeId,
                    projectType = x.Project.projectType,
                    DetailDescription = x.DetailDescription,
                    AnswerDescription = x.ProjectDetailsAnswers.Select(a => a.AnswerDescription)
                }).ToList();
    

    但如果你坚持从项目中查询:

            var result = _dbContext.Projects.SelectMany(x=>x.ProjectDetails) //<==use SelectMany
                .Select(x => new ProjectDetailViewModel
                {
                    projectTypeId = x.Project.ProjectTypeId,
                    projectType = x.Project.projectType,
                    DetailDescription = x.DetailDescription,
                    AnswerDescription = x.ProjectDetailsAnswers.Select(a => a.AnswerDescription)
                }).ToList();