Exists/in/any/all/contains operator
applicable scenario : Used to determine the elements in the set, further narrowing the scope.
Any
Description: Used to determine whether an element in the collection satisfies a condition; (If the condition is NULL, the collection returns true if it is not empty, otherwise false). There are 2 forms, namely the simple form and the conditional form.
1. Simple form:
Only customers who do not have an order are returned:
var q = in db. Customers where!c.orders.any () select C;
The generated SQL statement is:
SELECT [T0]. [CustomerID], [t0]. [CompanyName], [t0]. [Contactname],[t0]. [ContactTitle], [t0]. [Address], [t0]. [City], [t0]. [Region],[t0]. [PostalCode], [t0]. [Country], [t0]. [Phone], [t0]. [Fax] As [t0]WHERE not (EXISTS ( WHERE [t1].[ CustomerID] = [T0]. [CustomerID]))
2. With conditional form:
Return only the categories that have at least one product that is out of stock:
var q = in db. Categories where c.products.any (p = p.discontinued) select C;
The generated SQL statement is:
As [t0]WHERE EXISTS (and ([t1].[ CategoryID] = [T0]. [CategoryID]) )
All
Description: Used to determine whether all elements in the set satisfy a certain condition;
1. With conditional form
var q = in db. Customers where c.orders.all (o = o.shipcity = = c.city) select C;
Statement Description: This example returns all orders shipped to customers in their city or customers who have not placed an order.
Contains
Description: Used to determine whether an element is contained in a collection; It is a connection operation to two sequences.
string[] Customerid_set = "Fissa"}; Select O). ToList ();
Statement Description: Find orders for the three customers "Arout", "Bolid" and "Fissa". An array is defined, using contains in LINQ to SQL, and all the CustomerID are included in the array, that is, all CustomerID are within the set. That is in. You can also place the definition of an array in a LINQ to SQL statement. Like what:
var q = (in db. The Orders where ( select O). ToList ();
The not contains is reversed:
var q = (in db. Orders where! ( select O). ToList ();
1. Contains an object:
var order = (indb. Orders where O.orderid = = 10248 select O). First (); var q = db. Customers.where (P = p.orders.contains (order)). ToList (); foreach (foreach (//do Something}}
Statement Description: This example uses contain to find which customer contains an order with a OrderID of 10248.
2. Contains multiple values:
string[] Cities = "Paris"}; var q = db. Customers.where (p=>cities. Contains (p.city)). ToList ();
Statement Description: This example uses contains to find customers in the City of Seattle, London, Paris or Vancouver.
Exists/in/any/all/contains operator