condition
Required. An expression that determines whether the values for the current item in the collection are included in the output collection. The expression must evaluate to a Boolean value or the equivalent of a Boolean value. If the condition evaluates to True, the element is included in the query result; otherwise, the element is excluded from the query result.
Remarks
The Where clause enables you to filter query data by selecting only elements that meet certain criteria. Elements whose values cause the Where clause to evaluate to True are included in the query result; other elements are excluded. The expression that is used in a Where clause must evaluate to a Boolean or the equivalent of a Boolean, such as an Integer that evaluates to False when its value is zero. You can combine multiple expressions in a Where clause by using logical operators such as And, Or, AndAlso, OrElse, Is, and IsNot.
By default, query expressions are not evaluated until they are accessed—for example, when they are data-bound or iterated through in a For loop. As a result, the Where clause is not evaluated until the query is accessed. If you have values external to the query that are used in the Where clause, ensure that the appropriate value is used in the Where clause at the time the query is executed. For more information about query execution, see Writing Your First LINQ Query.
You can call functions within a Where clause to perform a calculation or operation on a value from the current element in the collection. Calling a function in a Where clause can cause the query to be executed immediately when it is defined instead of when it is accessed. For more information about query execution, see Writing Your First LINQ Query.
Example 1
The following query expression uses a From clause to declare a range variable cust for each Customer object in the customers collection. The Where clause uses the range variable to restrict the output to customers from the specified region. The For Each loop displays the company name for each customer in the query result.
VB
Sub DisplayCustomersForRegion(ByVal customers As List(Of Customer),
ByVal region AsString)
Dim customersForRegion = From cust In customers
Where cust.Region = region
ForEach cust In customersForRegion
Console.WriteLine(cust.CompanyName)
NextEndSub
Example 2
The following example uses And and Or logical operators in the Where clause.
VB
PrivateSub DisplayElements()
Dim elements As List(Of Element) = BuildList()
' Get a list of elements that have an atomic number from 12 to 14,' or that have a name that ends in "r".Dim subset = From theElement In elements
Where (theElement.AtomicNumber >= 12And theElement.AtomicNumber < 15) _
Or theElement.Name.EndsWith("r")
OrderBy theElement.Name
ForEach theElement In subset
Console.WriteLine(theElement.Name & " " & theElement.AtomicNumber)
Next' Output:' Aluminum 13' Magnesium 12' Silicon 14' Sulfur 16EndSubPrivateFunction BuildList() As List(Of Element)
ReturnNew List(Of Element) From
{
{New Element With {.Name = "Sodium", .AtomicNumber = 11}},
{New Element With {.Name = "Magnesium", .AtomicNumber = 12}},
{New Element With {.Name = "Aluminum", .AtomicNumber = 13}},
{New Element With {.Name = "Silicon", .AtomicNumber = 14}},
{New Element With {.Name = "Phosphorous", .AtomicNumber = 15}},
{New Element With {.Name = "Sulfur", .AtomicNumber = 16}}
}
EndFunctionPublicClass Element
PublicProperty Name AsStringPublicProperty AtomicNumber AsIntegerEndClass
Bạn có thể tìm thấy nguồn cho nội dung này trên GitHub, nơi bạn cũng có thể tạo và xem lại các vấn đề và yêu cầu kéo. Để biết thêm thông tin, hãy xem hướng dẫn dành cho người đóng góp của chúng tôi.
Ý kiến phản hồi về .NET
.NET là một dự án nguồn mở. Chọn liên kết để cung cấp ý kiến phản hồi:
Tham gia chuỗi buổi gặp gỡ để xây dựng các giải pháp AI có thể mở rộng dựa trên các trường hợp sử dụng trong thế giới thực với các nhà phát triển và chuyên gia đồng nghiệp.