concept query operator in category .net

appears as: query operators, query operator, query operators, The query operators
C# in Depth

This is an excerpt from Manning's book C# in Depth.

The most obvious situation where you’re forced to use dot notation is when you’re calling a method such as Reverse or ToDictionary that isn’t represented in query expression syntax at all. But even when you use a query operator that’s supported by query expressions, it’s quite possible for the overload you want to be unavailable.

Personally, I enjoy writing query operators. There are interesting technical challenges, but it rarely requires a huge amount of code, and the results can be elegant. In this section, we’ll look at some of the ways you can make your custom operators behave efficiently and predictably, followed by a full sample for selecting a random element from a sequence.

LINQ in Action

This is an excerpt from Manning's book LINQ in Action.

We will focus on LINQ to Objects, LINQ to SQL, and LINQ to XML in this book, but LINQ is open to new data sources. The three main LINQ providers discussed in this book are built on top of a common LINQ foundation. This foundation consists of a set of building blocks including query operators, query expressions, and expression trees, which allow the LINQ toolset to be extensible.

3.3. 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.

Listing 3.7. C# query expression that uses query operators (QueryExpressionWithOperators.csproj)
var authors = from distinctAuthor in ( from book in SampleData.Books where book.Title.Contains("LINQ") from author in book.Authors.Take(1) select author) .Distinct() select new {distinctAuthor.FirstName, distinctAuthor.LastName};

3.3.1. 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.

Figure 3.2. A LINQ query represented as a factory where query operators are machines and sequences are the material.