Good day,
I am using ASP. NET MVC with C# and entity framework. I have three tables in my codefirst 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:
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; }
}
What I would like to do is to use entity framework with lambda expresions to load a 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
}
What I have so far is this code:
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();
I receive this error: > cannot convert string to List in this code . n.AnswerDescription.Select(b =>b.AnswerDescription).ToList()
What am I doing wrong? Can you, if possible, provide me some code that works for the purpose of loading my viewmodel with data?
, the problem comes
Thanks