LINQ query and counting subqueries in C #?

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 have Numbers listed when starting foreach ?

  • how can I test it (I mean by writing code that tells me the number of iterations)

+7
source share
7 answers

It will be repeated 6 times. Once for Where and once for an element for Max .

Code demonstrating this:

 private static int count = 0; public static IEnumerable<int> Regurgitate(IEnumerable<int> source) { count++; Console.WriteLine("Iterated sequence {0} times", count); foreach (int i in source) yield return i; } int[] Numbers = new int[5] { 5, 2, 3, 4, 5 }; IEnumerable<int> sequence = Regurgitate(Numbers); var query = from a in sequence where a == sequence.Max(n => n) select a; 

He will print "Iterated Sequence 6 Times."

We could make a more universal shell, more flexible if you plan to use it to experiment with other cases:

 public class EnumerableWrapper<T> : IEnumerable<T> { private IEnumerable<T> source; public EnumerableWrapper(IEnumerable<T> source) { this.source = source; } public int IterationsStarted { get; private set; } public int NumMoveNexts { get; private set; } public int IterationsFinished { get; private set; } public IEnumerator<T> GetEnumerator() { IterationsStarted++; foreach (T item in source) { NumMoveNexts++; yield return item; } IterationsFinished++; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public override string ToString() { return string.Format( @"Iterations Started: {0} Iterations Finished: {1} Number of move next calls: {2}" , IterationsStarted, IterationsFinished, NumMoveNexts); } } 

This has several advantages over another function:

  • It records both the number of iterations started, the number of completed iterations, and the total number of times that all sequences have been increased.
  • You can create different instances to transfer different base sequences, which allows you to check multiple sequences for each program, and not just one when a static variable is used.
+3
source

Here is how you can check the quantity

 void Main() { var Numbers= new int[5]{5,2,3,4,5}.Select(n=> { Console.Write(n); return n; }); var query = from a in Numbers where a== Numbers.Max (n => n) select a; foreach (var element in query) { var v = element; } } 

It displays here

 5 5 2 3 4 5 2 5 2 3 4 5 3 5 2 3 4 5 4 5 2 3 4 5 5 5 2 3 4 5 
+3
source

Here's how you can evaluate a quick count of the number of times a collection is enumerated: wrap your collection in CountedEnum<T> and an increment counter on each yield return , for example:

 static int counter = 0; public static IEnumerable<T> CountedEnum<T>(IEnumerable<T> ee) { foreach (var e in ee) { counter++; yield return e; } } 

Then change the array declaration to this,

 var Numbers= CountedEnum(new int[5]{5,2,3,4,5}); 

run the request and print counter . At your request, the code prints 30 ( link to ideone ), which means that your collection of five elements has been listed six times.

+3
source

The number of iterations should be equal to query.Count() .

So, to count the elements as a result of the first query.

If you are asking for something else, please clarify.

EDIT

After clarification:

if you are looking for a common iteration counter in the provided code, there will be 7 iterations (for this particular case).

 var query = from a in Numbers where a== Numbers.Max (n => n) //5 iterations to find MAX among 5 elements select a; 

and

 foreach (var element in query) Console.WriteLine (element); //2 iterations over resulting collection(in this question) 
+1
source

How many times the number of Numbers is listed when starting foreach

In short, your code is morally equivalent:

 foreach(int a in Numbers) { // 1. I've gotten rid of the unnecessary identity lambda. // 2. Note that Max works by enumerating the entire source. var max = Numbers.Max(); if(a == max) Console.WriteLine(a); } 

So, we list the following points:

  • One sequence listing for the outer loop ( 1 ).
  • One listing of the sequence for each of its members ( Count ).

Thus, we list Count + 1 times.

You can bring this to 2 by raising the Max query outside the loop by entering local.

how can I check it (I mean by writing code that tells me the iteration number)

It will not be easy with a raw array. But you can write your own enumerated implementation (possibly wrapped in an array) and add some tools to the GetEnumerator method. Or, if you want to go deeper, go over the whole pig and write your own tool counter on MoveNext and Current .

+1
source

Counting through a public property also gives 6.

 private static int ncount = 0; private int[] numbers= new int[5]{5,2,3,4,5}; public int[] Numbers { get { ncount++; Debug.WriteLine("Numbers Get " + ncount.ToString()); return numbers; } } 

This returns a score of up to 2.
It makes sense, but I would not think about it.

 int nmax = Numbers.Max(n => n); var query = from a in Numbers where a == nmax //notice MAX he should also get his value somehow //where a == Numbers.Max(n => n) //notice MAX he should also get his value somehow select a; 
+1
source

It will be repeated 6 times. Once for Where and once for an element for Max .

Define and initialize the count variable outside the foreach loop and increment the count variable as count++ inside the loop to get the number of times the enumeration.

0
source

All Articles