Refactor multiple 'Access to modified closure' in same code block

I have the following code:

foreach (var current in listToUpdate)
{
    var bridgeMapping = model.BridgeMappingLipperGlobal.Where(p => p.NameODS == current.NameODS).FirstOrDefault();
    bridgeMapping.FundTechCat = model.FundTechCat.Where(p => p.idFundTechCat == current.FundTechCat.Id).FirstOrDefault();
}

R# correctly warns me about the usage of "current" in the lambda expressions. After refactoring it with "Copy to local variable" I get this:

foreach (var current in listToUpdate)
{
    var bridgeMappingLipperGlobalBE = current;
    var bridgeMapping = model.BridgeMappingLipperGlobal.Where(p => p.NameODS == bridgeMappingLipperGlobalBE.NameODS).FirstOrDefault();
    bridgeMapping.FundTechCat = model.FundTechCat.Where(p => p.idFundTechCat == current.FundTechCat.Id).FirstOrDefault();
}

Better would be if R# would replace all the "current" references with "bridgeMappingLipperGlobalBE" so that the result would be:

foreach (var current in listToUpdate)
{
    var bridgeMappingLipperGlobalBE = current;
    var bridgeMapping = model.BridgeMappingLipperGlobal.Where(p => p.NameODS == bridgeMappingLipperGlobalBE.NameODS).FirstOrDefault();
    bridgeMapping.FundTechCat = model.FundTechCat.Where(p => p.idFundTechCat == bridgeMappingLipperGlobalBE.FundTechCat.Id).FirstOrDefault();
}

1 comment
Comment actions Permalink

Hello Gabriel
     I've created a request in our tracker: http://www.jetbrains.net/jira/browse/RSRP-124798 and you're welcome to monitor its status.

Andrey Serebryansky

Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"

0

Please sign in to leave a comment.