I have two tables:
CREATE TABLE Thing (
Id int,
Name nvarchar(max)
);
CREATE TABLE SubThing (
Id int,
Name nvarchar(max),
ThingId int (foreign key)
);
I want to select all SubThings enumerated Things and set them to the ThingViewModel.
Thing ViewModel is simple:
public class ThingViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<SubThingViewModel> SubThings { get; set; }
}
SubThingViewModel:
public class SubThingViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
I already select the Thing entries as follows:
List<ThingViewModel> things = null;
things = _context.Things.OrderBy(b => b.Name)
.Select(b => new ThingViewModel
{
Id = b.Id,
Name = b.Name
}).ToList();
How to add SubThings to request and ViewModel?
source
share