Gets or sets a value that specifies what conditions must be true for a record to be included in the retrieved data.
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
You use the property to specify the conditions for the record to be returned from a query. The syntax for the property is the same as the syntax for a LINQ Where clause in C#.
You specify an expression that results in a Boolean value, and if the expression evaluates to true for a given row, the row is included in the result set. Expressions are composed of column names, comparison operators, and values to compare to, as shown in the following example:
<asp:LinqDataSource ... Where="Price > 50"...>
To specify multiple expressions linked by logical AND or OR operators, you use && as a logical AND operator and || as a logical OR operator, as shown in the following example:
<asp:LinqDataSource ... Where="Price > 50 && Price < 100"...> <asp:LinqDataSource ... Where="Price <= 50 || Price >= 100"...>
If you want to test a property against a literal string value, the literal string value must be enclosed in double quotation marks. To do this in markup, enclose the clause value in single quotation marks, as shown in the following example:
<asp:LinqDataSource ... Where='Category = "Sports"' ... >
To test against a literal string value in code, use escape characters that are appropriate to the language you are using in order to insert double quotation marks, as shown in the following example:
If you want to test whether a string is greater than or less than another string, you must use methods of the String class instead of using < or > operators between the column name and the string value. The following examples show how to select rows that have Category values that are less than, less than or equal to, greater than, or greater than or equal to "Sports":
<asp:LinqDataSource ... Where='Category.CompareTo("Sports") < 0' ... > <asp:LinqDataSource ... Where='Category.CompareTo("Sports") <= 0' ... > <asp:LinqDataSource ... Where='Category.CompareTo("Sports") > 0' ... > <asp:LinqDataSource ... Where='Category.CompareTo("Sports") >= 0' ... >
You can also use other methods of the String class, such as StartsWith, EndsWith, and Contains. For more information about how to compare strings, see Comparing Strings. For more information about Where clause syntax, see C# Operators and where clause (C# Reference).
In addition to filtering based on static values that you define when you create the Web page, you can filter based on dynamic values that are evaluated at run time. In that case, you include a named parameter in the property that acts as a placeholder for the value. You then add a parameter that has the matching name to the WhereParameters collection.
Alternatively, you can set the AutoGenerateWhereClause property to true and define the parameters in the WhereParameters collection. When the AutoGenerateWhereClause property is true, you do not have to include the named parameters in the property. Instead, the LinqDataSource control automatically generates the Where clause from the parameters in the WhereParameters property.
For more information about how to filter data, see Walkthrough: Selecting and Filtering a Subset of Data with the LinqDataSource and GridView Controls.
The following example shows how to filter the data returned from a query based on a static condition.
The following example shows how to filter the data based on a value that is provided by the user at run time. In this example a DropDownList control and a GridView control are displayed on the page. When the user selects one of the values in the DropDownList control, the LinqDataSource control selects from the Products table only rows that have UserPrice values equal to the selected value. The GridView control then displays the filtered data.
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server"> <asp:ListItem Value="0"></asp:ListItem> <asp:ListItem Value="25"></asp:ListItem> <asp:ListItem Value="100"></asp:ListItem> <asp:ListItem Value="400"></asp:ListItem> </asp:DropDownList> <asp:LinqDataSource ContextTypeName="ExampleDataContext" TableName="Products" Where="Price>@UserPrice" ID="LinqDataSource1" runat="server"> <WhereParameters> <asp:ControlParameter Name="UserPrice" DefaultValue="0" ControlID="DropDownList1" Type="Int32" /> </WhereParameters> </asp:LinqDataSource> <asp:GridView DataSourceID="LinqDataSource1" ID="GridView1" runat="server"> </asp:GridView>
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.