6.12. Returning Specific Elements of a Collection

Problem

You need to retrieve a specific element or groups of elements from a collection.

Solution

Call any of the partitioning methods listed in Table 6-2, such as First or Single, to return the desired element from the collection.

How It Works

Not all of the extension methods found in the System.Linq.Enumerable namespace relate directly to a query clause, such as those covered in the previous recipes of this chapter. The methods listed in Table 6-2 fall in this category and provide functionality to extract a single element from a collection. If you use any of these methods as part of a query, the query will execute immediately.

Table 6.2. Common Partitioning Methods
MethodDescription
ElementAt ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach 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.