Home Access object from outer select clause in LINQ statement
Reply: 0

Access object from outer select clause in LINQ statement

user1049
1#
user1049 Published in April 22, 2018, 12:51 am

Suppose I have the following LINQ Statement. I would like to find a way to use "r.Portfolio" while I am in the 3rd select clause (see comment in the code):

positionsToOptimize
    .Select(p => p)
    .SelectMany(r => r.Positions)
    .Select(pos => new OptimizedPosition() {
        CalculationType = TCalculationType.Absolute,
        Position = pos.NetPositionAmount,
        SecurityID = pos.SecurityID,
        Portfolio = r.Portfolio        // <- How do I do this? (Portfolio is not in 'pos')
    });

Essentially I am trying to replace the following foreach nested loop:

var lst = new List<OptimizedPosition>();
foreach (var r in positionsToOptimize) {
    foreach (var pos in r.Positions) {
        lst.Add(new OptimizedPosition() {
            CalculationType = TCalculationType.Absolute,
            Position = pos.NetPositionAmount,
            SecurityID = pos.SecurityID,
            SystemCode = r.SystemCode
        });
    }
}

return lst;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30658 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO