dknaack
dknaack

Reputation: 60516

LINQ Select and Convert Entites to ViewModels

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!

Upvotes: 4

Views: 13618

Answers (6)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236288

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

Upvotes: 1

themarcuz
themarcuz

Reputation: 2583

To keep your own style of writings:

var q = from p in dbContext.Parents
    where p.Id == 123
    select new ParentViewModel
    {
        Id = p.Id,
        Children = (from c in p.Children
                    select new ChildViewModel()
                    {
                        Id = c.Id,
                        Toys = (from t in c.Toys
                                select new ToyViewModel() 
                                {
                                    Id = t.Id,
                                    Name = t.Name
                                }).ToList()
                    }).ToList()
    };

And I would call Toy instead of Toys and ToyViewModel instead of ToysViewModel

Upvotes: 0

Lukazoid
Lukazoid

Reputation: 19426

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

Upvotes: 3

Dave Bish
Dave Bish

Reputation: 19646

Do you mean this?

var q = from p in dbContext.Parents
    where p.Id = 123
    select new ParentViewModel
    {
       Id = p.Id,
       Children = p.Children.Select(c => new ChildViewModel
       {
           Id = c.Id,
           Toys = c.Toys.Select(t => new ToysViewModel
           {
               Id = t.Id
           }).ToList()
       }).ToList()
    };

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46008

I would use AutoMapper to convert my entites to models. It supports collections nicely.

Upvotes: 0

Jon
Jon

Reputation: 437664

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

Upvotes: 12

Related Questions