LINQ - 如何在select语句中为子对象提供对其父对象的引用?

Sha*_*awn 3 c# linq

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

        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();
Run Code Online (Sandbox Code Playgroud)

我希望每个Capitalcall对象都有一个返回其FundEntity的引用.如果不创建循环并手动设置每个循环,这是否可行?

Ste*_*han 7

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();
Run Code Online (Sandbox Code Playgroud)

那应该给你参考.