0

I have 2 lists, allCharges and selectedCharges, where each item in selectedCharges is contained in allCharges.

I want to create a third list (called charges) that is a copy of all charges, but with a boolean property of "Selected" for each item set to true if the charge is in the list of selected charges.

This is fine if I am only doing this once; however, I am trying to perform the same operation five times, rewriting "charges" each time while saving "charges" to my "typeSettingsList".

IList<TypeSettings> typeSettingsList = new List<TypeSettings>();

var typeId = 1;
while (typeId < 6)
{
    var links = _linkChargeTypeRepo.Query().Where(l => l.TypeId == typeId);
    var allCharges = _chargeRepo.GetAll().ToList();

    var selectedCharges = links.Select(l => l.ChargeType).ToList();
    selectedCharges.ForEach(c => c.Selected = true);

    var nonSelectedCharges = allCharges.Except(selectedCharges).ToList();
    nonSelectedCharges.ForEach(c => c.Selected = false);

    var charges = nonSelectedCharges.Concat(selectedCharges).ToList();

    var settingsWithType = new TypeSettings
    {
        Type = _typeRepo.Get(typeId),
        Charges = charges
    };

    typeSettingsList.Add(settingsWithType);

    typeId++;
}

return settingsWithType;

My problem is that each "Charges" object in my typeSettingsList ends up getting overwritten with the charges object that is created on the last iteration, even though the variable is declared inside the while loop (and should therefore be a new object reference with each iteration).

Is this just an incorrect understanding of how variables inside while loops should work?

How can I make it so my "charges" list isn't overwritten with each iteration?

2
  • selectedCharges is a list variable and TypeSettings (I guess) is not a value type. Each list is new but objects it contains are reused Commented Oct 16, 2014 at 16:08
  • @AdrianoRepetti I'm not sure I understand what you are saying. My TypeSettings object is an object that contains a Type and a List<ChargeType>. Is there some reason why this should not work? Commented Oct 16, 2014 at 16:26

1 Answer 1

4

The problem is that you do not "materializecharges`: this

var charges = nonSelectedCharges.Concat(selectedCharges);

is an IEnumerable<Charge> with deferred evaluation. By the time you get to evaluate Charges from the typeSettingsList, the loop is over, so enumerating the IEnumerable<Charge> returns the results for the last value of typeId (i.e. typeId = 5).

Add ToList() to fix this problem:

var charges = nonSelectedCharges.Concat(selectedCharges).ToList();

Edit: Another problem is that links is using typeId, which is modified in the loop. You should define a new variable inside the loop to capture the state of typeId during the specific iteration, like this:

var typeId = 1;
while (typeId < 6)
{
    var tmpTypeId = typeId;
    var links = _linkChargeTypeRepo.Query().Where(l => l.TypeId == tmpTypeId);
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Oops, I copy and pasted my code slightly incorrectly. I had the ToList() on there to begin with. I added it now and am getting the same result.
@MitchVz That is strange. I would expect Type to be off, because typeId is modified in the loop (assuming that there is some deferred execution there, which may or may not be true). But everything else there uses ToList(), so it should simply work. If you are certain that you are running your latest code there, try adding some debug output in the loop to log the key statistics of the charges that you get, perhaps even the lists themselves, and see if the issue occurs at the stage of collecting the charges in the loop.
Yeah, the Type is just about the only thing that is working :) The object that is returned has five different types, but the same Charges list for each type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.