0

I have alot of tables/entities that I will convert to a list of ParentDTO's that have list of ChildDTO's. I use ParentDTO and ChildDTO to be present as a spreadsheet where ParentDTO are the row and ChildDTO are the column. If VALUE in one column are null then it won't be saved in database. If value != null then I create new entity in database if MasterId / ValueID are null and if ValueId != null, then I will update value. And if I set value to null, then I delete entity with the current ValueID.

ParentDTO
public int MasterID { get;set; }  // Point to MasterId
public string Name {get;set;}
public bool IsLocked {get;set}
public List<ChildDTO> Childs {get;set;}

ChildDTO
public int? MasterID {get;set}  // Point to MasterId
public int Year {get;set;}
public int Value {get;set;}
public int? ValueID {get;set}   // Point to MasterValueId


MasterEntity
public int Id {get;set}
..
public PropertyOneId {get;set;}   // In my masterentity I have alot of property links
public PropertyOne PropertyOne {get;set}
public List<ChildDataEntity> ChildsEntities {get;set;}

ChildEntity
public int Id {get;set;}   // ValueID in ChildDTO
public int Year {get;set;}
public int Value {get;set;}
public int MasterEntityID {get;set;}  

Is there a way to create ParentDTO and ChildDTO in one singleline of code?

List<ParentDTO> pDTO = await _db.MasterEntity.
    .Include(i => i.ProptertyOne)
         .ThenInclude(i => i.PropertyOne.PropertyOneChild)
    .Include(i => i.ChildEntity)
    .Select(r => new ParentDTO
    {
        MasterId = r.Id,
        Name = r.Name,
        Childs = ??????
    }
    .ToListAsync();

Or do I have to make a list of ParentDTO first and then run a foreach(var x in pDTO) and then create list of ChildDTO for each pDTO?

0 Answers0