简体   繁体   English

实体框架查询以使用键值对填充视图模型,键值对包含太多参数

[英]Entity Framework query to populate viewmodel with keyvaluepairs, keyvaluepairs with too many arguments

I have three model classes in my code-first model. 我的代码优先模型中有三个模型类。

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

As you can see a project has many details. 如您所见,项目有很多细节。

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

And a projectDetail has many answers: 一个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; }
}

This is my viewmodel 这是我的视图模型

public class ProjectViewModel
{
    public Guid Id { get; set; }
    public List<KeyValuePair<string, List<string>>> Details { get; set; }
}

I wanted to populate that viewmodel with a keyvaluepair 我想用一个键值对填充该视图模型

var data = _dbContext.Project
                    .Select(p => new ProjectViewModel
                                  {
                                      Id = p.Id,
                                      Details = p.ProjectDetails
                                                 .Select(pd => new KeyValuePair<string, List<string>>(pd.DetailDescription, pd.ProjectDetailsAnswers.Select(pda => pda.AnswerDescription).ToList())
                                                 .ToList()
                                  })
                    .ToList();

However I get this error 但是我得到这个错误

KeyValuePair does not contain a constructor that takes that many arguments. KeyValuePair不包含接受那么多参数的构造函数。

What am I doing wrong? 我究竟做错了什么?

Thanks 谢谢

I had the same problem. 我有同样的问题。 Turns out there was a typo "bad escape sequence" a couple lines up in my code in a string. 原来,我的代码中有几个输入错误的“错误的转义序列”。 Totally unrelated to the KeyValuePair line. 与KeyValuePair行完全无关。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM