在select子句中,是否可以在select子句中指向其他级别的对象,或者是否有任何特殊的关键字要指向。如果不是,如何使用select子句来解决这个问题。
list.Select(o => new ParentClass {
ID = o.ID,
ChildClass = o.Childs.Select(p => new ChildClass {
Parent = @this,
ID = p.id
}).ToList()
});发布于 2014-08-15 07:11:38
不,你不能。因为还没有创建ParentClass实例。你需要下面这样的东西。
list.Select(o =>
{
var parent = new ParentClass
{
ID = o.ID
};
parent.ChildClass = o.Childs
.Select(p =>
new ChildClass
{
Parent = parent,
ID = p.id
})
.ToList();
return parent;
});发布于 2014-08-15 07:11:40
您不能使用对象初始化程序来完成此操作。
list.Select(o =>
{
var pc = new ParentClass();
pc.ID = o.ID;
pc.ChildClass = o.Childs.Select(p => new ChildClass
{
Parent = pc,
ID = p.id
}).ToList();
return pc;
});https://stackoverflow.com/questions/25322470
复制相似问题