Επεξεργασία

Κοινή χρήση μέσω


LinqDataSource.Where Property

Definition

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

public:
 property System::String ^ Where { System::String ^ get(); void set(System::String ^ value); };
public string Where { get; set; }
member this.Where : string with get, set
Public Property Where As String

Property Value

A string that is used to create the Where clause.

Implements

Examples

The following example shows how to filter the data returned from a query based on a static condition.

<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Where="Price > 50"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Where="Price > 50"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>

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

Remarks

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

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.

Applies to