简体   繁体   English

EntityDataSource WHERE IN

[英]EntityDataSource WHERE IN

I have a asp:EntityDataSource我有一个asp:EntityDataSource

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" 
    EntitySetName="Student" Where="it.Age = 12 or it.Age = 13"> 
</asp:EntityDataSource>

now I need to show more ages.现在我需要显示更多的年龄。 Is there a way to write that shorter?有没有办法写得更短?

My try:我的尝试:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" 
    EntitySetName="Student" Where="it.Age IN (11,12,13,14)"> 
</asp:EntityDataSource>

but that throws an error但这会引发错误

The right argument of the set expression must be of CollectionType.集合表达式的正确参数必须是 CollectionType。

The right way to use IN clause on Where attribute of EntityDataSource is putting curly brace block surrounding all values like array declaration: Where EntityDataSource Where属性上使用IN子句的正确方法是将花括号块放在所有值(如数组声明)周围:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" 
    EntitySetName="Student" Where="it.Age IN {11,12,13,14}"> 
</asp:EntityDataSource>

The reason behind usage of curly braces possibly depends on ObjectQuery which receives typed query against model on given context, which may accepts array of values.使用大括号的原因可能取决于ObjectQuery ,它接收针对给定上下文中的模型的类型查询,它可能接受值数组。 In other words, the IN clause have equivalent on LINQ format as this:换句话说,IN 子句在 LINQ 格式上具有等价性,如下所示:

int[] ages = new int[] {11, 12, 13, 14};
var query = MyEntities.Student.Where(it => it.Age.Contains(ages));

And generated SQL command would become:生成的 SQL 命令将变为:

SELECT * FROM [MyEntities].[Student] WHERE Age IN (11,12,13,14)

Other possible reason is server control syntax in ASPX page markup follows XSLT convention regarding attribute value templates , which interprets curly brace blocks as string-value expression and parentheses treated as method parameter/expression enclosure (see XSL transformation details ).其他可能的原因是 ASPX 页面标记中的服务器控件语法遵循关于属性值模板的XSLT 约定,它将花括号块解释为字符串值表达式,并将括号视为方法参数/表达式外壳(请参阅XSL 转换详细信息)。

Similar issue:类似问题:

EntityDataSource Where Clause in VB.NET VB.NET 中的 EntityDataSource Where 子句

Additional reference:补充参考:

Creating IN Queries 创建 IN 查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM