我正在尝试做这样的事情:

        List<FundEntity> entities = this.tFunds
            .Select(f => new FundEntity() {
               ID = f.fundID,
               Name = f.name,
               CapitalCalls = f.tCapitalCalls
                    .Select(cc => new CapitalCall() {
                        ID =  cc.capitalCallID,
                        Description = cc.description,
                        FundEntity = // Should be the newly created Fund Entity object
                    }).ToList()
            }).ToList();


我希望每个Capitalcall对象都具有对它的FundEntity的引用。是否可以在不创建循环并手动设置每个循环的情况下实现?

最佳答案

List<FundEntity> entities = this.tFunds
        .Select(f =>
        {
           var parent = new FundEntity() {
               ID = f.fundID,
               Name = f.name,
               };
           parent.CapitalCalls = f.tCapitalCalls
                    .Select(cc => new CapitalCall() {
                    ID =  cc.capitalCallID,
                    Description = cc.description,
                    FundEntity =parent // Should be the newly created Fund Entity object
                });
            return parent;
        }.ToList()
        ).ToList();


那应该给你参考。

09-27 13:57