Elements Operators

Some extension methods enable you to get the instance of a specified item in a sequence. The first one is Single, and it gets the instance of only the item that matches the specified condition. The following code gets the instance of the only product whose product name is Mozzarella:

Try    Dim uniqueElement = products.Single(Function(p) p.                                        ProductName = "Mozzarella")Catch ex As InvalidOperationException    'The item does not existEnd Try

Single takes a lambda expression as an argument in which you can specify the condition that the item must match. It returns an InvalidOperationException if the item does not exist in the sequence (or if more than one element ...

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.