score:1

Accepted answer

I would rewrite your inner query to just return all the values in one request.

void Load()
{ 
  var inventory = SaveLoad.Load<List<string>>("Inventory")
    .Join(itemDatabase.itemDataList, name=>name, item=>item.name, (name,item)=>item);
  // Use one of the following three methods:
  instance.AddRange(inventory);
  // or if instance is just a List of items then...
  instance = inventory.ToList();
  // or if there is no AddRange, and instance is not just a List:
  foreach(var item in inventory)
  {
    instance.Add(item);
  }
}

Alternatively, create yourself a GetInventory method:

IEnumerable<Item> GetInventory()
{ 
  return SaveLoad.Load<List<string>>("Inventory")
    .Join(itemDatabase.itemDataList, name=>name, item=>item.name, (name,item)=>item);
}

Then you can do whatever you want with it in your Load method.

score:1

This is happening because your loadReference is defined elsewhere, so you are just changing pointers around (basically copying over the previously loaded item each time).

void Load()
{ 
    List<string> itemStrings = SaveLoad.Load<List<string>>("Inventory");

    foreach(string nameString in itemStrings)
    {
        var item = itemDatabase.itemsDatabaseList.Where(obj => obj.name == nameString).First();
        bool wasAdded = instance.Add(item);
    }
}

Related Articles