Same query producing different results between two executions in C#

Encoder QR in C# Same query producing different results between two executions

Listing 3.5 Same query producing different results between two executions
Paint QR Code ISO/IEC18004 In C#.NET
Using Barcode generation for VS .NET Control to generate, create QR Code ISO/IEC18004 image in VS .NET applications.
www.OnBarcode.com
Scanning QR Code In Visual C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
using System; using System.Linq; static class QueryReuse { static double Square(double n) { Console.WriteLine("Computing Square("+n+")..."); return Math.Pow(n, 2); } public static void Main() { int[] numbers = {1, 2, 3};
Code 128 Code Set C Generation In C#
Using Barcode creator for .NET framework Control to generate, create ANSI/AIM Code 128 image in .NET framework applications.
www.OnBarcode.com
Generating EAN 128 In Visual C#
Using Barcode encoder for VS .NET Control to generate, create UCC-128 image in VS .NET applications.
www.OnBarcode.com
LINQ building blocks
ECC200 Generation In Visual C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
www.OnBarcode.com
Printing 2D In C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create Matrix 2D Barcode image in .NET applications.
www.OnBarcode.com
var query = from n in numbers select Square(n); foreach (var n in query) Console.WriteLine(n); for (int i = 0; i < numbers.Length; i++) numbers[i] = numbers[i]+10; Console.WriteLine("- Collection updated -"); foreach (var n in query) Console.WriteLine(n); } }
Printing PDF417 In C#.NET
Using Barcode generation for .NET Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
USPS Confirm Service Barcode Maker In C#
Using Barcode printer for .NET Control to generate, create USPS Confirm Service Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Here we reuse the query object after changing the underlying collection. We add 10 to each number in the array before iterating again on the query. As expected, the results are not the same for the second iteration:
QR Code Encoder In Java
Using Barcode generator for Android Control to generate, create QR Code image in Android applications.
www.OnBarcode.com
Create Quick Response Code In None
Using Barcode generator for Font Control to generate, create QR Code image in Font applications.
www.OnBarcode.com
Computing Square(1)... 1 Computing Square(2)... 4 Computing Square(3)... 9 - Collection updated Computing Square(11)... 121 Computing Square(12)... 144 Computing Square(13)... 169
Painting EAN / UCC - 14 In Visual Studio .NET
Using Barcode creator for Reporting Service Control to generate, create USS-128 image in Reporting Service applications.
www.OnBarcode.com
Barcode Encoder In Java
Using Barcode generation for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
The second iteration executes the query again, producing new results. Forcing immediate query execution As you ve seen, deferred execution is the default behavior. Queries are executed only when we request data from them. If you want immediate execution, you have to request it explicitly. Let s say that we want the query to be executed completely, before we begin to process its results. This would imply that all the calls to the Square method happen before the results are used. Here is how the output should look without deferred execution:
Print ECC200 In None
Using Barcode maker for Office Excel Control to generate, create Data Matrix 2d barcode image in Excel applications.
www.OnBarcode.com
Painting GS1 - 12 In Objective-C
Using Barcode drawer for iPad Control to generate, create UPC Code image in iPad applications.
www.OnBarcode.com
Introducing query operators
Create Data Matrix ECC200 In .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
USS Code 39 Recognizer In Visual C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Computing Square(1)... Computing Square(2)... Computing Square(3)... 1 4 9
Print EAN-13 Supplement 5 In Java
Using Barcode drawer for Android Control to generate, create EAN-13 Supplement 5 image in Android applications.
www.OnBarcode.com
Code 128 Code Set A Printer In .NET
Using Barcode encoder for .NET Control to generate, create Code 128 image in .NET applications.
www.OnBarcode.com
We can achieve this by adding a call to ToList another extension method from the System.Linq.Enumerable class to our code sample:
Barcode Scanner In Java
Using Barcode Control SDK for BIRT reports Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Barcode Creator In VB.NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
foreach (var n in query.ToList()) Console.WriteLine(n);
With this simple modification, our code s behavior changes radically. ToList iterates on the query and creates an instance of List<double> initialized with all the results of the query. The foreach loop now iterates on a prefilled collection, and the Square method is not invoked during the iteration. Let s go back to our DisplayProcesses example and continue analyzing the query. The Where, OrderByDescending, and Select methods used in listing 3.1 are iterators. This means for example that the enumeration of the source sequence provided as the first parameter of a call to the Where method won t happen before we start enumerating the results. This is what allows delayed execution. You ll now learn more about the extension methods provided by the System.Linq.Enumerable class.
Introducing query operators
We ve used extension methods from the System.Linq.Enumerable class several times in our code samples. We ll now spend some time describing them more precisely. You ll learn how such methods, called query operators, are at the heart of the LINQ foundation. You should pay close attention to query operators, because you ll use them the most when writing LINQ queries. We ll first define what a query operator is, before introducing the standard query operators.
What makes a query operator
Query operators are not a language extension per se, but an extension to the .NET Framework Class Library. Query operators are a set of extension methods that perform operations in the context of LINQ queries. They are the real elements that make LINQ possible.
LINQ building blocks
Before spending some time on iterators, we were looking at the Where method that is used in the following code sample:
var processes = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20*1024*1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name=process.ProcessName });
Call to Where
Let s take a deeper look at the Where method and analyze how it works. This method is provided by the System.Linq.Enumerable class. Here again is how it s implemented, as we showed in listing 3.2:
public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, Boolean> predicate) foreach { loop foreach (TSource element in source) { if (predicate(element)) Filter source yield return element; Return } elements }
Note that the Where method takes an IEnumerable<T> as an argument. This is not surprising, because it s an extension method that gets applied to the result of the call to Process.GetProcesses, which returns an IEnumerable<Process> as we ve seen before. What is particularly interesting at this point is that the Where method also returns an IEnumerable<T>, or more precisely an IEnumerable<Process> in this context. Here is how the Where method works:
Copyright © OnBarcode.com . All rights reserved.