LINQ - How can I give a child object in a select statement a reference to a parent?

I am trying to do something like this:

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

I would like each Capitalcall object to return a link to its FundEntity. Is this possible without creating a loop and setting each of them manually?

+3
source share
2 answers
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();

This should give you a link.

+7
source

Is this LINQ in memory or LINQ for Entities / SQL?

  • In the first case, you can simply create an object and then set its property CapitalCallsimperatively (as in the example from Stephan)

  • , , LINQ ( , , ). Entity, ...

+2

Source: https://habr.com/ru/post/1751241/


All Articles