LINQ Select and Convert Entites to ViewModels

12,690

Solution 1

If the relations are properly modeled in the database, then

select new ParentViewModel
{
   Id = p.Id,
   Children = p.Children.Select(c => new ChildViewModel {
       Id = c.Id,
       Toys = c.Toys.Select(t => new ToyViewModel { ... }).ToList()
   }).ToList()
};

Solution 2

For clarity I would prefer to break it down into functions like this:

public static ParentViewModel AsViewModel(Parent parent)
{
    var childViewModels = parent.Children.Select(AsViewModel).ToList();
    return new ParentViewModel { Id = parent.Id, Children = childViewModels };
}

public static ChildViewModel AsViewModel(Child child)
{
    var toyViewModels = child.Toys.Select(AsViewModel).ToList();
    return new ChildViewModel { Id = child.Id, Toys = toyViewModels };
}

public static ToyViewModel AsViewModel(Toy toy)
{
    return new ToyViewModel { Id = toy.Id, Name = toy.Name };
}

Then it can be used like so:

var q = dbContext.Parents.Where(p => p.Id == 123).Select(AsViewModel);

Solution 3

Consider using some auto-mapping tool like Automampper.

Define somewhere mappings:

Mapper.CreateMap<Parent, ParentViewModel>();
Mapper.CreateMap<Child, ChildViewModel>();
Mapper.CreateMap<Toys, ToysViewModel>();

And your code

 Parent parent = GetParentById(123); // use LINQ to get parent
 ParentViewModel pvm = Mapper.Map<ParentViewModel>(parent);
Share:
12,690
dknaack
Author by

dknaack

Updated on June 16, 2022

Comments

  • dknaack
    dknaack almost 2 years

    I am not new to Linq but for any reason i cant solve this problem.

    Lets say i have the following Models. Clearly this are sample models.

    public class Parent
    {
        public int Id { get; set; }
        public List<Child> Children { get; set; }
        public DateTime CreateDate { get; set; }
    }
    
    public class Child
    {
        public int Id { get; set; }
        public List<Toys> Toys { get; set; }
        public DateTime CreateDate { get; set; }
    }
    
    public class Toys
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public DateTime CreateDate { get; set; }
    }
    

    What i want is to convert them into ViewModels. Here how my ViewModels are defined

    public class ParentViewModel
    {
        public int Id { get; set; }
        public List<ChildViewModel> Children { get; set; }
    }
    
    public class ChildViewModel
    {
        public int Id { get; set; }
        public List<ToysViewModel> Toys { get; set; }
    }
    
    public class ToysViewModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    

    My Problem

    How can i select and convert my models into the viewModels. Here is what i already have tried.

    var q = from p in dbContext.Parents
            where p.Id = 123
            select new ParentViewModel
            {
               Id = p.Id,
               Children = ???
            };
    

    How can get the List of Children and their Toys ?

    Thanks in advance!