Skip to main content

Where - Simple 1

This sample uses where to find all elements of an array less than 5.

Public Sub Linq1()
    Dim numbers() = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}
    Dim lowNums = From n In numbers _
        Where n < 5 _
        Select n

    Console.WriteLine("Numbers < 5:")

    For Each x In lowNums
        Console.WriteLine(x)
    Next
End Sub


Result:
Numbers < 5:
4
1
3
2
0


Where - Simple 2

This sample uses where to find all products that are out of stock.

Public Sub Linq2()
    Dim products = GetProductList()

    Dim soldOutProducts = From p In products _
        Where p.UnitsInStock = 0 _
        Select p

    Console.WriteLine("Sold out products: ")
    For Each prod In soldOutProducts
        Console.WriteLine(prod.ProductName & " is sold out!")
    Next
End Sub


Public Function GetProductList() As List(Of Product)
    If productList Is Nothing Then
        CreateLists()
    End If

    Return productList
End Function


Result:
Sold out products:
Chef Anton's Gumbo Mix is sold out!
Alice Mutton is sold out!
Thüringer Rostbratwurst is sold out!
Gorgonzola Telino is sold out!
Perth Pasties is sold out!


Where - Simple 3

This sample uses where to find all products that are in stock and cost more than 3.00 per unit.

Public Sub Linq3()
    Dim products = GetProductList()

    Dim expensiveInStockProducts = From p In products _
        Where p.UnitsInStock > 0 AndAlso p.UnitPrice > 3.0 _
        Select p

    Console.WriteLine("In-stock products that cost more than 3.00:")
    For Each prod In expensiveInStockProducts
        Console.WriteLine(prod.ProductName & "is in stock and costs more than 3.00.")
    Next
End Sub


Public Function GetProductList() As List(Of Product)
    If productList Is Nothing Then
        CreateLists()
    End If

    Return productList
End Function


Result:
In-stock products that cost more than 3.00:
Chaiis in stock and costs more than 3.00.
Changis in stock and costs more than 3.00.
Aniseed Syrupis in stock and costs more than 3.00.
Chef Anton's Cajun Seasoningis in stock and costs more than 3.00.
Grandma's Boysenberry Spreadis in stock and costs more than 3.00.
Uncle Bob's Organic Dried Pearsis in stock and costs more than 3.00.
Northwoods Cranberry Sauceis in stock and costs more than 3.00.
Mishi Kobe Nikuis in stock and costs more than 3.00.
Ikurais in stock and costs more than 3.00.
Queso Cabralesis in stock and costs more than 3.00.
Queso Manchego La Pastorais in stock and costs more than 3.00.
Konbuis in stock and costs more than 3.00.
Tofuis in stock and costs more than 3.00.
Genen Shouyuis in stock and costs more than 3.00.
Pavlovais in stock and costs more than 3.00.
Carnarvon Tigersis in stock and costs more than 3.00.
Teatime Chocolate Biscuitsis in stock and costs more than 3.00.
Sir Rodney's Marmaladeis in stock and costs more than 3.00.
Sir Rodney's Sconesis in stock and costs more than 3.00.
Gustaf's Knäckebrödis in stock and costs more than 3.00.
Tunnbrödis in stock and costs more than 3.00.
Guaraná Fantásticais in stock and costs more than 3.00.
NuNuCa Nuß-Nougat-Cremeis in stock and costs more than 3.00.
Gumbär Gummibärchenis in stock and costs more than 3.00.
Schoggi Schokoladeis in stock and costs more than 3.00.
Rössle Sauerkrautis in stock and costs more than 3.00.
Nord-Ost Matjesheringis in stock and costs more than 3.00.
Mascarpone Fabioliis in stock and costs more than 3.00.
Sasquatch Aleis in stock and costs more than 3.00.
Steeleye Stoutis in stock and costs more than 3.00.
Inlagd Sillis in stock and costs more than 3.00.
Gravad laxis in stock and costs more than 3.00.
Côte de Blayeis in stock and costs more than 3.00.
Chartreuse verteis in stock and costs more than 3.00.
Boston Crab Meatis in stock and costs more than 3.00.
Jack's New England Clam Chowderis in stock and costs more than 3.00.
Singaporean Hokkien Fried Meeis in stock and costs more than 3.00.
Ipoh Coffeeis in stock and costs more than 3.00.
Gula Malaccais in stock and costs more than 3.00.
Rogede sildis in stock and costs more than 3.00.
Spegesildis in stock and costs more than 3.00.
Zaanse koekenis in stock and costs more than 3.00.
Chocoladeis in stock and costs more than 3.00.
Maxilakuis in stock and costs more than 3.00.
Valkoinen suklaais in stock and costs more than 3.00.
Manjimup Dried Applesis in stock and costs more than 3.00.
Filo Mixis in stock and costs more than 3.00.
Tourtièreis in stock and costs more than 3.00.
Pâté chinoisis in stock and costs more than 3.00.
Gnocchi di nonna Aliceis in stock and costs more than 3.00.
Ravioli Angelois in stock and costs more than 3.00.
Escargots de Bourgogneis in stock and costs more than 3.00.
Raclette Courdavaultis in stock and costs more than 3.00.
Camembert Pierrotis in stock and costs more than 3.00.
Sirop d'érableis in stock and costs more than 3.00.
Tarte au sucreis in stock and costs more than 3.00.
Vegie-spreadis in stock and costs more than 3.00.
Wimmers gute Semmelknödelis in stock and costs more than 3.00.
Louisiana Fiery Hot Pepper Sauceis in stock and costs more than 3.00.
Louisiana Hot Spiced Okrais in stock and costs more than 3.00.
Laughing Lumberjack Lageris in stock and costs more than 3.00.
Scottish Longbreadsis in stock and costs more than 3.00.
Gudbrandsdalsostis in stock and costs more than 3.00.
Outback Lageris in stock and costs more than 3.00.
Flotemysostis in stock and costs more than 3.00.
Mozzarella di Giovanniis in stock and costs more than 3.00.
Röd Kaviaris in stock and costs more than 3.00.
Longlife Tofuis in stock and costs more than 3.00.
Rhönbräu Klosterbieris in stock and costs more than 3.00.
Lakkalikööriis in stock and costs more than 3.00.
Original Frankfurter grüne Soßeis in stock and costs more than 3.00.


Where - Drilldown

This sample uses where to find all customers in Washington and then uses the resulting sequence to drill down into their orders.

Public Sub Linq4()
    Dim customers = GetCustomerList()

    Dim waCustomers = From c In customers _
        Where c.Region = "WA" _
        Select c

    Console.WriteLine("Customers from Washington and their orders:")
    For Each cust In waCustomers
        Console.WriteLine("Customer {0}: {1}", cust.CustomerID, cust.CompanyName)
        For Each ord In cust.Orders
            Console.WriteLine("  Order {0}: {1}", ord.OrderID, ord.OrderDate)
        Next
    Next
End Sub


Result:
Customers from Washington and their orders:
Customer LAZYK: Lazy K Kountry Store
  Order 10482: 3/21/1997 12:00:00 AM
  Order 10545: 5/22/1997 12:00:00 AM
Customer TRAIH: Trail's Head Gourmet Provisioners
  Order 10574: 6/19/1997 12:00:00 AM
  Order 10577: 6/23/1997 12:00:00 AM
  Order 10822: 1/8/1998 12:00:00 AM
Customer WHITC: White Clover Markets
  Order 10269: 7/31/1996 12:00:00 AM
  Order 10344: 11/1/1996 12:00:00 AM
  Order 10469: 3/10/1997 12:00:00 AM
  Order 10483: 3/24/1997 12:00:00 AM
  Order 10504: 4/11/1997 12:00:00 AM
  Order 10596: 7/11/1997 12:00:00 AM
  Order 10693: 10/6/1997 12:00:00 AM
  Order 10696: 10/8/1997 12:00:00 AM
  Order 10723: 10/30/1997 12:00:00 AM
  Order 10740: 11/13/1997 12:00:00 AM
  Order 10861: 1/30/1998 12:00:00 AM
  Order 10904: 2/24/1998 12:00:00 AM
  Order 11032: 4/17/1998 12:00:00 AM
  Order 11066: 5/1/1998 12:00:00 AM


Where - Indexed

This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value.

Public Sub Linq5()
    Dim digits() = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}

    Dim shortDigits = digits.Where(Function(digit, index) digit.Length < index)

    Console.WriteLine("Short digits:")
    For Each d In shortDigits
        Console.WriteLine("The word {0} is shorter than its value.", d)
    Next
End Sub


Result:
Short digits:
The word five is shorter than its value.
The word six is shorter than its value.
The word seven is shorter than its value.
The word eight is shorter than its value.
The word nine is shorter than its value.