suppose I have this query :
int[] Numbers= new int[5]{5,2,3,4,5};
var query = from a in Numbers
where a== Numbers.Max (n => n) //notice MAX he should also get his value somehow
select a;
foreach (var element in query)
Console.WriteLine (element);
-
How many times does
Numbersis enumerated when running theforeach? -
how can I test it ( I mean , writing a code which tells me the number of iterations)
It will be iterated 6 times. Once for the
Whereand once per element for theMax.The code to demonstrate this:
It will print “Iterated sequence 6 times”.
We could make a more general purpose wrapper that is more flexible, if you’re planning to use this to experiment with other cases:
This has several advantages over the other function: