I have 4 classes
Class 1:
Public class user
{
Public int Id {get;set;}
Public string username {get;set;}
Public ICollection<Project>
projects{get;set;}
Public
ICollection<Projectmembers>
Projectmembers{get;set;}
}
Class 2:
Public class project
{
Public int Id{get;set;}
Public string projectname
{get;set;}
Public int userid {get;set;}
Public ICollection<ProjectReq
{get;set;}
[Foreignkey(nameof(userid))]
Public user userfield{get;set;}
}
Class 3:
Public class ProjectReq
{
Public int Id {get;set;}
Public int projectid {get;set;}
Public string role {get;set;}
[Foreignkey(nameof(projectid))]
Public project proj {get;set;}
Public ICollection<Projectmembers>
{get;set;}
}
Class 4:
Public class Projectmembers
{
Public int Id {get;set;}
Public int reqid {get;set;}
Public int userid {get;set;}
[Foreignkey(nameof(reqid))]
Public project projectReq
{get;set;}
[Foreignkey(nameof(userid))]
Public user userfield {get;set;}
}
How can I map project class to the below view model;
Public class Projectmemberview
{
Public string
projectname{get;set;}
Public string username{get;set;}
Public string role{get;set;}
}
I have used linq and got result to project entity model please help me with mapping it to the Projectmemberview model
2
Answers
Assuming you had lists of each object type (
Project
,User
,ProjectReq
), you could iterate over theProject
list, and for each one create aProjectMemberView
like so:This code is incomplete because of assumptions made, and may need error handling.
I tried as below:
I couldn’t understand how you want to map the role property (the source of role property is in ProjectReq Class,and in your project class there’s a collection of ProjectReq),So I ignored the property,it may help if you could check your model and explain to me .
in startup:
In controller:
The result: