LinqDataSource.Where 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定一個值,指定記錄必須為何條件才能包含在檢索資料中。
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
屬性值
一個用來建立 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"...>
若要指定多個由邏輯ANDOR或運算子連結的表達式,請將 作為邏輯 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 的方法,而不是在欄位名稱和字串值之間使用 < 或 > 運算子。 以下範例說明如何選擇類別值小於、小於或等於、大於或大於或等於「運動」的列:
<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 類別的其他方法,例如 StartsWith、 EndsWith和 Contains。 關於如何比較字串的更多資訊,請參見 「字串比較」。 欲了解更多關於 Where 子句語法的資訊,請參閱 C# 運算子 與 where 子句。
除了根據你建立網頁時定義的靜態值來篩選外,你也可以根據執行時評估的動態值來篩選。 在這種情況下,你在屬性中加入一個命名參數 Where ,作為值的佔位符。 接著你為集合加入一個名稱相符 WhereParameters 的參數。
或者,你也可以將屬性設 AutoGenerateWhereClause 為 , true 並在集合中 WhereParameters 定義參數。 當屬性 AutoGenerateWhereClause 為 true時,你不必在屬性中包含已命名的參數 Where 。 相反地,控制 LinqDataSource 項會自動從屬性中的 WhereParameters 參數產生 Where 子句。
欲了解更多如何篩選資料的資訊,請參閱「 Walkthrough:使用 LinqDataSource 與 GridView 控制項選擇與篩選資料子集」。