使用这样的父子设置:父母有子列表。
public sealed class Parent
{
public Parent(DateTime recordTime, int randomValue, bool isAccepted, List<Child> children)
{
RecordTime = recordTime;
RandomValue = randomValue;
IsAccepted = isAccepted;
Children = adjustments;
}
public DateTime RecordTime { get; private set; }
public int RandomValue { get; private set; }
public bool IsAccepted { get; private set; }
public List<Child> Children { get; private set; }
}
public sealed class Child
{
public Child(int randomChildValue, bool isPositive, Parent parent)
{
RandomChildValue = randomChildValue;
IsPositive = isPositive;
Parent = parent;
}
public int RandomChildValue { get; private set; }
public bool IsPositive { get; private set; }
public Parent Parent { get; private set; }
}
使用LINQ Select扩展方法根据其他集合投影父级和子级。如何将父项传递给子项。我知道我可以使用.Select((x,index) => new Parent....
获取父级索引在下面的linq语句中我传递null,但是可以传递父级吗?
var parentCollection = neighborCollection.Select(x => new Parent(x.Time, (int)x.ItemsProcessed, x.UserAccepted,
x.subCollection.Select(y => new Child((int)y.Sub, y.IsPositive, null)).ToList()));
答案 0 :(得分:0)
只需让您的父构造函数将自己指定为每个子项的父项。但是,由于Parent是私有的,因此您必须在父构造函数
中重新生成子列表Children = children.Select(c => new Child(c.RandomChildValue, c.IsPositive, this)).ToList()