❔ Put results of LINQ query into collection
I'm trying to put the results of my query into collection filteredItems, and then use a foreach to loop through it and display the items in the collection. How would I do this?
2 Replies
IEnumeralbe is read-only, you can't add items to it
Use List for a collection you can expand. If it needs to be IEnumerable say to store the result of a Linq expression, resolve the query to a collection with
ToList()
or ToArray()
Also no need to put the object type between the collection and Add. It's not a member and the generic type takes care of handling the type in methods of the colleciton.
Talking shiur the first loop using filteredItems
As for the second loop with lstItems
, assuming it's a built-in collection type such as List, you can add to it except itemList
is not defined in that scope and like before, no need to go through an Items member to add to it.
itemList
also wouldn't have a DisplayText member since only individual items have it and this is an enumerable.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.