Restriction Operators

LINQ offers an operator named Where that allows filtering query results according to the specified condition. For example, continuing with the previous examples of a collection of products, the following code returns only non-discontinued products:

Dim query = From prod In products            Where prod.Discontinued = False            Select prod

The same result can be accomplished by invoking a same-named extension method that works as follows:

Dim result = products.Where(Function(p) p.Discontinued = False).            Select(Function(p) p)

Where supports lots of operators on the line that are summarized in Table 23.1.

TABLE 23.1 Operators Supported by Where ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.