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"...>
若要指定由逻辑 AND 或 OR 运算符链接的多个表达式,请使用 && 逻辑 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 集合。
或者,可以将属性true设置为AutoGenerateWhereClause集合中的WhereParameters参数并定义参数。
AutoGenerateWhereClause当属性为true时,不必在Where属性中包含命名参数。 相反,控件 LinqDataSource 会从属性中的 WhereParameters 参数自动生成 Where 子句。
有关如何筛选数据的详细信息,请参阅 演练:使用 LinqDataSource 和 GridView 控件选择和筛选数据子集。