-1

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

smn.tino
  • 2,272
  • 4
  • 32
  • 41
ev vk
  • 345
  • 1
  • 4
  • 18
  • We need to see your `MyViewModel` definition (and it's child type definitions) – Shyju Jul 17 '18 at 17:01
  • You have multiple ProjectDetails in EF and a single DetailDescription in the view model. What do you want for the value? – M B Jul 17 '18 at 17:01
  • you are right, I would like to have in my viewmodel one string with my projectdetail.DetailDescription and my list of ProjectDetailsAnswer.AnswerDescription – ev vk Jul 17 '18 at 17:17
  • in this way I would have something like "this is my description" and then <"answer1", "answer2", "answer 3"...> "my second description" <"answer1", "answer4", "answer6"> – ev vk Jul 17 '18 at 17:19
  • Also why do you use IList instead of List in your class definitions? – johannespartin Jul 17 '18 at 17:57
  • Your class `Project` doesn't have a propertry named `Project`, but you access it in the `Select()` statement! How? And, please format your code - it's not readable, give spaces in the correct places, line breaks where needed, give correct names `Guid` and not `guid`, uniform naming convetion - you even have properties named x that you write them y! If you want we'll help you, please help we to understand you. – Chayim Friedman Jul 17 '18 at 18:02
  • @johannesp I just used a tutorial and for the navigation properti, there was an IList. Is it bad? – ev vk Jul 17 '18 at 18:27
  • 1
    @johannesp - Using IList is a best practice for public methods and properties https://stackoverflow.com/questions/400135/listt-or-ilistt – Vinit Jul 17 '18 at 18:37

1 Answers1

1

if DetailDescription is just a string in your viewmodel, but not a IList, so I guess it's a ProjectDetailViewModel, so your linq should be:

        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();

but if you insist on query from projects:

        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();
Dongdong
  • 2,208
  • 19
  • 28