我有一个两级嵌套子集合,我将其投影到 DTO 中。我的问题也适用于常规的父->子关系:
// There are two types on Parent and two types of Child classes,
// one is EF model, and another is DTO (here I show only one, for simplicity)
public class Parent
{
public int Id {get;set;}
public IEnumerable<Child> Children {get;set;}
}
public class Child
{
public int Id {get;set;}
public Parent Parent {get;set;}
}
var list = context.Set<Parent>().Select(p=> new DTO.Parent
{
Id = p.Id
Children = (p.Children.Select(c=> new DTO.Child
{
Id=c.Id,
Parent = ?
}))
});
在进行投影时是否可以为子对象分配父引用?