Considering elements as a list of objects (already loaded in memory, not a DB query) and given the following portion of code:
var filteresElements = elements.Where(el => el.Flag == true).Select(el.Id).ToList();
What is the time complexity of this row? Is elements looped twice or just once?
Clarification: the question is not related to a real life problem, when I wrote the question I thought at elements as a generic List but you can elaborate your question on whatever Iterable object you like: the point of the question is how LINQ expression iterate over in memory data structures.
elements? Please post a minimal reproducible example.elementsactually is. Iterating a dictionary is a lot more expensive than iterating an array or list. The container's iterator implementation also matters - what if the container is a weird tree onFlagthat returns two separate lists? Or it's aLookupor other grouping onFlagin which case allTrueelements are in the same internal array?TElement[]buffers internally for performance reasons - data locality matters and the N of indirect RAM access can be orders of magnitude bigger than the N of working with a buffer in the CPU cache.