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);
}
}
Source: stackoverflow.com
Related Articles
- Using LINQ within a 'foreach' loop only running once
- Linq statement only executing once and returning zero on second try and above in foreach loop
- Using LINQ in foreach loop declaration
- Simplify conventional foreach nested loop using linq & lambda expression
- Resharper, linq within foreach loop
- convert foreach loop to linq code
- Using Linq to loop through all controls only get the first control
- Using a LINQ query instead of a foreach loop
- Using Linq to return each item only once
- Converting foreach loop to LINQ query breaks code
- Using linq within a ForEach statement
- Conditions on foreach loop using Linq
- Using only Linq or Lambda, how do I combine both pieces of code to return List<Entity> e?
- Convert if-else within a foreach loop to LINQ
- Using Linq Aggregate or ForEach to update properties for a running total
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Using union inside a foreach loop for linq to sql
- Convert the code from Foreach loop to Linq
- Use a foreach loop within a foreach loop in a linq query
- How to find the items that are repeated only once in the list using LINQ
- Getting rid of outer foreach loop using linq or lambda and ensuring no duplicates are added to list
- c# linq to entities using method based queries - trying to select where the object appears only once
- How to convert the following foreach loop to linq code format?
- How to read string array value using linq where clause instead of foreach loop
- C# Reading Dictionary<String, List<String>> using LINQ or foreach loop
- Using a foreach loop on LINQ query for searching
- Convert string[] to int[] in one line of code using LINQ
- How do I find the text within a div in the source of a web page using C#
- How to select values within a provided index range from a List using LINQ
- Using LINQ for an object that only has GetEnumerator()
- Cannot convert type 'System.Linq.IQueryable<System.Data.Linq.Binary> to byte
- c# bindingsource and linq
- How can I calculate an integral?
- Find elements which aren't in other list
- LINQ Generic IQueryable<T> WHERE Clause
- N-Ary/Multiple-List Cartesian Product
- Verify if a list (or a sublist of that list) of decimal values can equal a certain sum
- Cannot apply indexing with [] to an expression of type 'method group' SinglePageApp1. Get["/"] Nancy
- Linq, lambda expression
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists
- LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])
- Running a regular expression on a String using Linq
- Difference between PredicateBuilder<True> and PredicateBuilder<False>?
- ForEach with index in C#
- When to use the asynschronus methods with EF Core?
- C# Pass Lambda Expression as Method Parameter
- Format LINQ code in VS2010
- Getting and Setting SQL Timeout?
- Paging using Linq
- IIS 7.0 vs 7.5 Site Microsoft.Web.Administration.Site BindingCollection