編集

次の方法で共有


LinqDataSource.AutoGenerateWhereClause Property

Definition

Gets or sets a value that indicates whether the LinqDataSource control dynamically creates a Where clause based on values defined in the WhereParameters collection.

public:
 property bool AutoGenerateWhereClause { bool get(); void set(bool value); };
public bool AutoGenerateWhereClause { get; set; }
member this.AutoGenerateWhereClause : bool with get, set
Public Property AutoGenerateWhereClause As Boolean

Property Value

true if the LinqDataSource control will create the Where clause; otherwise, false. The default is false.

Implements

Examples

The following example shows a LinqDataSource control with the AutoGenerateWhereClause set to true. A GridView control is bound to the LinqDataSource control to display the data that is returned from the query. A DropDownList control is included that is populated with three values. A parameter is included in the WhereParameters collection with the name set to Category, which matches one of the properties of the data object. Its ControlID property is set to the ID of the DropDownList control. The LinqDataSource control automatically creates the Where property to filter records based on the value that the user selects from the DropDownList control. The query returns the records whose Category property matches the value that the user has selected from the DropDownList control.

<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server">
    <asp:ListItem Value="Sports"></asp:ListItem>
    <asp:ListItem Value="Garden"></asp:ListItem>
    <asp:ListItem Value="Auto"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    AutoGenerateWhereClause="true"
    ID="LinqDataSource1" 
    runat="server">
    <WhereParameters>
        <asp:ControlParameter 
            Name="Category" 
            ControlID="DropDownList1" 
            Type="String" />
    </WhereParameters>
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server">
    <asp:ListItem Value="Sports"></asp:ListItem>
    <asp:ListItem Value="Garden"></asp:ListItem>
    <asp:ListItem Value="Auto"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    AutoGenerateWhereClause="true"
    ID="LinqDataSource1" 
    runat="server">
    <WhereParameters>
        <asp:ControlParameter 
            Name="Category" 
            ControlID="DropDownList1" 
            Type="String" />
    </WhereParameters>
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>

Remarks

When you set the AutoGenerateWhereClause property to true, the LinqDataSource control dynamically creates the Where clause from the parameters in the WhereParameters collection. Each parameter that you add to the WhereParameters collection must have its Name property set to a value that matches a property in the data object that is being queried. The automatically generated Where clause will check whether the value specified in the WhereParameters collection equals the value of the matching property in the data object. If you provide more than one parameter, the parameters are linked with a logical AND operation. Parameters that contain null or an empty value are not included in the Where clause.

The automatically generated Where clause can test only for equality and can link parameters only with the AND operation. Do not set the AutoGenerateWhereClause property to true if you have to add a condition that does not test for equality or if you have to relate parameters with the OR operation. You can accomplish these tasks by setting the AutoGenerateWhereClause property to false and adding placeholders in the Where property for each parameter in the WhereParameters collection. In the Where property, preface each placeholder name with the @ symbol.

You do not set the Where property when the AutoGenerateWhereClause property is true, because the parser dynamically creates the Where clause. The LinqDataSource control throws an exception if the AutoGenerateWhereClause property is true and the Where property is assigned values.

Applies to