LinqDataSource.Where 屬性

定義

取得或設定值,這個值指定針對要併入擷取之資料的資料錄,必須為 true 的條件。

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

屬性值

String

用於建立 Where 子句的字串。

實作

範例

下列範例示範如何根據靜態條件篩選從查詢傳回的資料。

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

下列範例示範如何根據使用者在執行時間提供的值來篩選資料。 在此範例中, DropDownList 控制項和 GridView 控制項會顯示在頁面上。 當使用者選取控制項中的 DropDownList 其中一個值時, LinqDataSource 控制項只會從 Products 資料表中選取的值等於所選取值的資料列 UserPrice 。 控制項 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>
<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>

備註

您可以使用 Where 屬性來指定要從查詢傳回之記錄的條件。 屬性的 Where 語法與 C# 中 LINQ Where 子句的語法相同。

您可以指定產生布林值的運算式,如果運算式評估 true 為指定資料列,則資料列會包含在結果集中。 運算式是由要比較的資料行名稱、比較運算子和值所組成,如下列範例所示:

<asp:LinqDataSource ... Where="Price > 50"...>  

若要指定邏輯或 OR 運算子連結的多個運算式,您可以使用 && 做為邏輯 AND AND 運算子和 || 邏輯 OR 運算子,如下列範例所示:

<asp:LinqDataSource ... Where="Price > 50 && Price < 100"...>  
<asp:LinqDataSource ... Where="Price <= 50 || Price >= 100"...>  

如果您想要針對常值字串值測試屬性,常值字串值必須以雙引號括住。 若要在標記中執行這項操作,請將 Where 子句值括在單引號中,如下列範例所示:

<asp:LinqDataSource ... Where='Category = "Sports"' ... >  

若要針對程式碼中的常值字串值進行測試,請使用適合您用來插入雙引號之語言的逸出字元,如下列範例所示:

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

如果您想要測試字串是否大於或小於另一個字串,您必須使用 類別的方法 String ,而不是在資料行名稱和字串值之間使用 <> 運算子。 下列範例示範如何選取類別值小於、小於或等於、大於或等於 「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' ... >  

您也可以使用 類別的其他方法 String ,例如 StartsWithEndsWithContains 。 如需如何比較字串的詳細資訊,請參閱 比較字串。 如需 Where 子句語法的詳細資訊,請參閱 C# 運算子where 子句

除了根據您在建立網頁時定義的靜態值進行篩選之外,您還可以根據執行時間評估的動態值進行篩選。 在此情況下,您會在 Where 屬性中包含具名參數,做為值的預留位置。 接著,您會將具有相符名稱的參數新增至 WhereParameters 集合。

或者,您可以將 屬性設定 AutoGenerateWhereClausetrue ,並在集合中 WhereParameters 定義參數。 AutoGenerateWhereClause當 屬性為 true 時,您不需要在 屬性中包含 Where 具名參數。 相反地 LinqDataSource ,控制項會自動從 屬性中的 WhereParameters 參數產生 Where 子句。

如需如何篩選資料的詳細資訊,請參閱逐步解說 :使用 LinqDataSource 和 GridView 控制項選取和篩選資料子集

適用於