Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

LinqDataSource.Where Property

Gets or sets a value that specifies what conditions must be true for a record to be included in the retrieved data.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public string Where { get; set; }
<asp:LinqDataSource Where="String" />

Property Value

Type: System.String
A string that is used to create the Where clause.



You use the Where property to specify the conditions for the record to be returned from a query. The syntax for the Where 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 Where 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:

LinqDataSource1.Where = "Category = \"Sports\"";

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 Where 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 Where 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.

    Where="Price > 50"

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>
            Type="Int32" />

.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2015 Microsoft