如何在 EntityDataSource 中使用 CASE 语句进行排序?
我在 EntityDataSource 中使用 CASE
语句来进行自定义排序。考虑以下代码:
<asp:EntityDataSource ID="myEntityDataSource" runat="server"
ConnectionString="name=MySQLEntities1"
DefaultContainerName="MySQLEntities1"
EnableFlattening="False"
EntitySetName="Persons"
EntityTypeFilter="Persons"
OrderBy="it.[Pack],
CASE it.[Type]
WHEN 'MAN' THEN 1
WHEN 'VROUW' THEN 2
WHEN 'KIND' THEN 3
END,
it.[BirthDate] ASC" />
在 T-SQL 中,这将是一种完美的正常排序方式,但在 EntityDataSource 中使用时会引发以下异常:
查询语法无效。靠近标识符“it”,第 11 行,列 21.
如何在我的 EntityDataSource
中使用这种类型的排序?
I'm using a CASE
statement in my EntityDataSource to do custom sorting. Consider the following code:
<asp:EntityDataSource ID="myEntityDataSource" runat="server"
ConnectionString="name=MySQLEntities1"
DefaultContainerName="MySQLEntities1"
EnableFlattening="False"
EntitySetName="Persons"
EntityTypeFilter="Persons"
OrderBy="it.[Pack],
CASE it.[Type]
WHEN 'MAN' THEN 1
WHEN 'VROUW' THEN 2
WHEN 'KIND' THEN 3
END,
it.[BirthDate] ASC" />
In T-SQL this would be a perfecty normal way of sorting, but used in the EntityDataSource
it throws the following exception:
The query syntax is not valid. Near identifier 'it', line 11, column
21.
How can I get this type of sorting to work in my EntityDataSource
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今天我也在尝试同样的事情。我在 Entity SQL Reference 站点上发现您显然必须使用 CASE WHEN 且不能使用 CASE [value] WHEN 。这种方法虽然不像 T-SQL 中那样,但对我来说确实有效。因此,您的代码应如下所示:
MSDN 实体 SQL 参考:'CASE'
I was trying the very same thing today. I discovered on the Entity SQL Reference site that you apparently have to use
CASE WHEN
and cannot useCASE [value] WHEN
. This approach, although not as it would be in T-SQL, did work for me. So your code should look like this:MSDN Entity SQL Reference: 'CASE'