使用私有 setter 的 linq 有可能吗?

Is this possible with linq using private setters?

使用 parent 和 child 设置如下: parent 有一个 children.

列表
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 扩展方法基于其他集合投影 parent 和 children。如何将 parent 项传递给 child。我知道我可以使用 .Select((x,index) => new Parent.... 获取 parent 的索引 在下面的 linq 语句中我传递了 null,但是是否可以传递 parent?

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

只需让您的父级构造函数将自己指定为每个子级的父级即可。但是,由于 Parent 是私有的,因此您必须在父构造函数中重新生成子列表

Children = children.Select(c => new Child(c.RandomChildValue, c.IsPositive, this)).ToList()