All 시퀀스의 모든 요소가 조건을 충족하는 경우 연산자가 반환 true
됩니다.
Any 시퀀스의 요소가 조건을 충족하는 경우 연산자가 반환 true
됩니다.
예제 1
다음 예제에서는 하나 이상의 주문이 있는 고객 시퀀스를 반환합니다. 지정된 Where
에 어떤 /가 있는 경우 where
true
Customer
절은 Order
로 계산됩니다.
var OrdersQuery =
from cust in db.Customers
where cust.Orders.Any()
select cust;
Dim OrdersQuery = _
From cust In db.Customers _
Where cust.Orders.Any() _
Select cust
예제 2
다음 Visual Basic 코드는 주문을 하지 않은 고객 목록을 결정하고 해당 목록의 모든 고객에 대해 연락처 이름이 제공되도록 합니다.
Public Sub ContactsAvailable()
Dim db As New Northwnd("c:\northwnd.mdf")
Dim result = _
(From cust In db.Customers _
Where Not cust.Orders.Any() _
Select cust).All(AddressOf ContactAvailable)
If result Then
Console.WriteLine _
("All of the customers who have made no orders have a contact name")
Else
Console.WriteLine _
("Some customers who have made no orders have no contact name")
End If
End Sub
Function ContactAvailable(ByVal contact As Object) As Boolean
Dim cust As Customer = CType(contact, Customer)
Return (cust.ContactTitle Is Nothing OrElse _
cust.ContactTitle.Trim().Length = 0)
End Function
예제 3
다음 C# 예제에서는 주문이 "C"로 시작하는 고객의 시퀀스를 반환합니다 ShipCity
. 또한 주문이 없는 고객도 반품에 포함됩니다. (설계상, 빈 시퀀스에 대해 All 연산자가 true
를 반환합니다.) 주문이 없는 고객은 Count
연산자를 사용하여 콘솔 출력에서 제거됩니다.
var custEmpQuery =
from cust in db.Customers
where cust.Orders.All(o => o.ShipCity.StartsWith("C"))
orderby cust.CustomerID
select cust;
foreach (Customer custObj in custEmpQuery)
{
if (custObj.Orders.Count > 0)
Console.WriteLine($"CustomerID: {custObj.CustomerID}");
foreach (Order ordObj in custObj.Orders)
{
Console.WriteLine($"\t OrderID: {ordObj.OrderID}; ShipCity: {ordObj.ShipCity}");
}
}