聚合或连接 linq to sql 查询中的字符串 (SQL Server)
给定一个像
ID | Name | City
1 | X | Y
2 | Z | Y
3 | W | K
我想生成这样的结果,就像
ID | Description
1 | Y (X, Z)
3 | K (W)
我尝试过类似的结果,
From C In Clients Group C By C.ID, C.City _
Into G = Group Select New With {.ID = ID, .Description = City & _
" (" & (From C In Clients Select C.Name).Aggregate(Function(X, Y) X & ", " & Y) & ")"}
这给了我一个错误“不支持查询运算符‘聚合’。” 也尝试过
From C In Clients Group C By C.ID, C.City _
Into G = Group Select New With {.ID = ID, .Description = City & _
" (" & String.Join((From C In Clients Select C.Name).ToArray, ", ") & ")"}
,这给了我错误“不支持 SQL 翻译”
那么,我该怎么做呢?
Given a table like
ID | Name | City
1 | X | Y
2 | Z | Y
3 | W | K
I want to produce a result like
ID | Description
1 | Y (X, Z)
3 | K (W)
I tried something like
From C In Clients Group C By C.ID, C.City _
Into G = Group Select New With {.ID = ID, .Description = City & _
" (" & (From C In Clients Select C.Name).Aggregate(Function(X, Y) X & ", " & Y) & ")"}
Which gave me an error "The query operator 'Aggregate' is not supported."
Also tried
From C In Clients Group C By C.ID, C.City _
Into G = Group Select New With {.ID = ID, .Description = City & _
" (" & String.Join((From C In Clients Select C.Name).ToArray, ", ") & ")"}
Which gave me the error "no supported translation to SQL"
So, how can i do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用 C# 破解了这个,它似乎给出了你想要的。我将把 VB 的翻译留给你。
I hacked this in C# and it seems to give what you want. I'll leave the translation to VB up to you.
该错误意味着您的 LINQ 操作无法在 TSQL 中的 SQL Server 上执行,正如您所编写的那样。
为了实现您想要的目标,您必须选择/评估尽可能多的基础数据,然后在第二步中执行聚合。两步或更多步骤的过程并不理想,但可以完成。
The error means that your LINQ operation cannot be performed on SQL Server in TSQL, as you've written it.
To achieve what you want, you'll have to select/evaluate as much of your base data as you can, and then perform the aggregation in a second step. A two-or-more step process isn't ideal, but it can be done.