4

我对 Linq 并不陌生,但出于任何原因我无法解决这个问题。

可以说我有以下型号。显然,这是示例模型。

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

我想要的是将它们转换为 ViewModels。这里我的 ViewModel 是如何定义的

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

我的问题

如何选择我的模型并将其转换为 viewModels。这是我已经尝试过的。

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

如何获得儿童及其玩具清单?

提前致谢!

4

6 回答 6

12

如果关系在数据库中正确建模,则

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()
};
于 2012-05-11T09:07:46.317 回答
3

为清楚起见,我更愿意将其分解为如下函数:

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

然后它可以像这样使用:

var q = dbContext.Parents.Where(p => p.Id == 123).Select(AsViewModel);
于 2012-05-11T09:11:20.847 回答
1

考虑使用一些自动映射工具,例如Automamper

定义某处映射:

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

还有你的代码

 Parent parent = GetParentById(123); // use LINQ to get parent
 ParentViewModel pvm = Mapper.Map<ParentViewModel>(parent);
于 2012-05-11T09:20:21.020 回答
0

我会使用AutoMapper将我的实体转换为模型。它很好地支持集合。

于 2012-05-11T09:09:33.317 回答
0

你是这个意思吗?

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()
    };
于 2012-05-11T09:10:18.363 回答
0

保持自己的写作风格:

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

我会打电话Toy而不是ToysToyViewModel而不是ToysViewModel

于 2012-05-11T09:17:56.323 回答