141: Simpler Standard Query Operators in Visual C#.NET

Generating PDF417 in Visual C#.NET 141: Simpler Standard Query Operators
TABLE 141: Simpler Standard Query Operators
PDF 417 Printer In C#.NET
Using Barcode encoder for .NET Control to generate, create PDF417 image in Visual Studio .NET applications.
Comment Type
Barcode Creator In C#
Using Barcode encoder for VS .NET Control to generate, create bar code image in Visual Studio .NET applications.
OfType<T>()
PDF 417 Creation In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
Description Forms a query over a collection that returns only the items of a particular type, where the type is identified in the type parameter of the OfType<T>() method call Combines two collections to form a superset of all the items in both collections The final collection does not include duplicate items even if the same item existed in both collections to start Combines two collections together to form a superset of both collections Duplicate items are not removed from the resultant collection Extracts the collection of items that exist in both original collections Filters out duplicate items from a collection so that each item within the resultant collection is unique
PDF417 Generator In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create PDF417 image in .NET framework applications.
Union()
PDF-417 2d Barcode Creation In VB.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create PDF 417 image in VS .NET applications.
Concat()
Generate Barcode In Visual C#
Using Barcode generation for Visual Studio .NET Control to generate, create barcode image in VS .NET applications.
Intersect()
Barcode Creator In C#.NET
Using Barcode generation for VS .NET Control to generate, create barcode image in .NET applications.
Distinct()
Code 3/9 Printer In C#
Using Barcode creation for .NET framework Control to generate, create Code 3 of 9 image in .NET framework applications.
Standard Query Operators
Code 128 Code Set C Generator In C#
Using Barcode encoder for Visual Studio .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
TABLE 141: Simpler Standard Query Operators (Continued)
Creating Data Matrix In C#
Using Barcode encoder for .NET Control to generate, create Data Matrix image in .NET framework applications.
Comment Type
Create Bar Code In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
SequenceEquals()
EAN128 Creation In .NET
Using Barcode printer for VS .NET Control to generate, create USS-128 image in .NET framework applications.
Description Compares two collections and returns a Boolean indicating whether the collections are identical, including the order of items within the collection (This is a very helpful message when testing expected results) Reverses the items within a collection so they occur in reverse order when iterating over the collection
Generating Code39 In .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 39 Extended image in ASP.NET applications.
Reverse()
Code 128 Code Set C Generator In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create Code 128 Code Set C image in .NET applications.
Included on SystemLinqEnumerable is a collection of aggregate functions that enumerate the collection and calculate a result Count is one example of an aggregate function already shown within the chapter
Code-39 Drawer In Visual Studio .NET
Using Barcode creator for VS .NET Control to generate, create Code 39 image in Visual Studio .NET applications.
TABLE 142: Aggregate Functions on SystemLinqEnumerable
Recognizing UPC-A In VS .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
Comment Type
Barcode Creation In VB.NET
Using Barcode printer for VS .NET Control to generate, create bar code image in .NET applications.
Count()
Reading Code-39 In VS .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET applications.
Description Provides a total count of the number of items within the collection Calculates the average value for a numeric key selector Computes the sum values within a numeric collection Determines the maximum value among a collection of numeric values Determines the minimum value among a collection of numeric values
Bar Code Generator In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
Average() Sum() Max()
Data Matrix Creation In Java
Using Barcode printer for Java Control to generate, create Data Matrix ECC200 image in Java applications.
Min()
Note that each method listed in Tables 141 and 142 will trigger deferred execution
ADVANCED TOPIC Queryable Extensions for IQueryable<T> One virtually identical interface to IEnumerable<T> is IQueryable<T> Because IQueryable<T> derives from IEnumerable<T>, it has all the members
14: Collection Interfaces with Standard Query Operators
of IEnumerable<T> but only those declared directly (GetEnumerator(), for example) Extension methods are not inherited, so IQueryable<T> doesn t have any of the Enumerable extension methods However, it has a similar extending class called SystemLinqQueryable that adds to IQueryable<T> virtually all of the same methods Enumerable added to IEnumerable<T> Therefore, it provides a very similar programming interface What makes IQueryable<T> unique is that it enables custom LINQ providers A LINQ provider subdivides expressions into their constituent parts Once divided, the expression can be translated into another language, serialized for remote execution, injected with an asynchronous execution pattern, and much more Essentially, LINQ providers allow for an interception mechanism into a standard collection API, and via this seemingly limitless functionality, behavior relating to the queries and collection can be injected For example, LINQ providers allow for the translation of a query expression from C# into SQL that is then executed on a remote database In so doing, the C# programmer can remain in her primary object-oriented language and leave the translation to SQL to the underlying LINQ provider Through this type of expression, programming languages are able to span the impedance mismatch between the object-oriented world and the relational database
SUMMARY
After introducing anonymous types, implicit variables, and collection initializers, this chapter described the internals of how the foreach loop works and what interfaces are required for its execution In addition, developers frequently filter a collection so there are fewer items and project the collection so the items take a different form Toward that end, this chapter discussed the details of how to use the standard query operators, common collection APIs on the SystemLinqEnumerable class, to perform collection manipulation In the introduction to standard query operators, I spent a few pages detailing deferred execution and how developers should take care to avoid unintentionally reexecuting an expression via a subtle call that enumerates
Summary
over the collection contents The deferred execution and resultant implicit execution of standard query operators is a significant quality, especially when the query execution is expensive Imagine, for example, a LINQ provider that returns data from a database, perhaps a database that is not running on the local computer Rather than retrieve the data from a database regardless of the selection criteria, the lambda expression would provide an implementation of IEnumerable<T> (more likely IQueryable<T> and an expression tree) that possibly includes context information such as the connection string, but not the data itself The data retrieval wouldn t occur until the call to GetEnumerator() or even MoveNext() However, the GetEnumerator() call is generally implicit, such as when iterating over the collection with foreach or calling an Enumerable method such as Count<T>() or Cast<T>() In such cases, developers need to be aware of the subtle call and avoid repeated calls to any expensive operation that deferred execution might involve For example, if calling GetEnumerator() involves a distributed call over the network to a database, avoid unintentional duplicate calls to iterations with Count() or foreach Listing 1422 appeared within an Advanced Topic section because of the complexity of calling multiple standard query operators one after the other Although requirements for similar execution may be common, it is not necessary to rely on standard query operators directly C# 30 includes query expressions, a SQL-like syntax for manipulating collections in a way that is frequently easier to code and read, as I ll show in the next chapter