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"...>
若要指定逻辑或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 ,例如 StartsWith、 EndsWith和 Contains。 有关如何比较字符串的详细信息,请参阅 比较字符串。 有关 Where 子句语法的详细信息,请参阅 C# 运算符 和 where 子句。
除了根据创建网页时定义的静态值进行筛选外,还可以根据运行时评估的动态值进行筛选。 在这种情况下,在 属性中包含 Where 一个命名参数,该参数充当值的占位符。 然后,将具有匹配名称的参数添加到 WhereParameters 集合。
或者,可以将 属性设置为 AutoGenerateWhereClausetrue ,并在集合中 WhereParameters 定义参数。
AutoGenerateWhereClause当 属性为 true时,不必在 属性中包含Where命名参数。 相反,控件 LinqDataSource 会从 属性中的 WhereParameters 参数自动生成 Where 子句。
有关如何筛选数据的详细信息,请参阅 演练:使用 LinqDataSource 和 GridView 控件选择和筛选数据子集。