Query Expression Syntax Examples: Filtering

 

The examples in this topic demonstrate how to use the Where and Where…Contains methods to query the AdventureWorks Sales Model using query expression syntax. Note, Where…Contains cannot be used as a part of a compiled query.

The AdventureWorks Sales model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

The examples in this topic use the following using/Imports statements:

[!CODE [DP L2E Examples#ImportsUsing](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#importsusing)]

Example

The following example returns all online orders.

[!CODE [DP L2E Examples#Where1](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#where1)]

Example

The following example returns the orders where the order quantity is greater than 2 and less than 6.

[!CODE [DP L2E Examples#Where2](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#where2)]

Example

The following example returns all red colored products.

[!CODE [DP L2E Examples#Where3](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#where3)]

Example

The following example uses the Where method to find orders that were made after December 1, 2003, and then uses the order.SalesOrderDetail navigation property to get the details for each order.

[!CODE [DP L2E Examples#WhereNavProperty](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#wherenavproperty)]

Example

The following example uses an array as part of a Where…Contains clause to find all products that have a ProductModelID that matches a value in the array.

[!CODE [DP L2E ArraysAndListsInQueries#1](../CodeSnippet/VS_Snippets_Data/dp l2e arraysandlistsinqueries#1)]

System_CAPS_ICON_note.jpg Note

As part of the predicate in a Where…Contains clause, you can use an Array, a List<T>, or a collection of any type that implements the IEnumerable<T> interface. You can also declare and initialize a collection within a LINQ to Entities query. See the next example for more information.

Example

The following example declares and initializes arrays in a Where…Contains clause to find all products that have a ProductModelID or Size that match values in the arrays.

[!CODE [DP L2E ArraysAndListsInQueries#2](../CodeSnippet/VS_Snippets_Data/dp l2e arraysandlistsinqueries#2)]

Queries in LINQ to Entities

Show: