使用 LINQ 将多行连接成单行(CSV 属性)
我正在寻找相当于 Sybase 的 LIST() 或 MySQL 的 group_concat() 的 LINQ
它将转换:
User Hobby
--------------
Bob Football
Bob Golf
Bob Tennis
Sue Sleeping
Sue Drinking
到:
User Hobby
--------------
Bob Football, Golf, Tennis
Sue Sleeping, Drinking
I'm looking for the LINQ equivalent to the Sybase's LIST() or MySQL's group_concat()
It'll convert:
User Hobby
--------------
Bob Football
Bob Golf
Bob Tennis
Sue Sleeping
Sue Drinking
To:
User Hobby
--------------
Bob Football, Golf, Tennis
Sue Sleeping, Drinking
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是 GroupBy 运算符。 您正在使用 LINQ to Objects 吗?
这是一个例子:
这就是分组 - 你能自己管理其余的吗?
That's the GroupBy operator. Are you using LINQ to Objects?
Here's an example:
That does the grouping - can you manage the rest yourself?
看看这个解决方案是否对您有帮助:
See if this solution helps you:
关于您问题的
_concat
方面,使用:bruno conde 和 Jon Skeet 的答案中的输出 foreach 可以变为:
... 您将获得您要求的精确结果输出格式(是的,我知道其他人已经解决了你的问题,但这很难抗拒!)
re the
_concat
aspect of your question, using:The outputting foreach in bruno conde and Jon Skeet's answers can become:
... and you'll get the precise result output format you asked for (yes, I know the others have already solved your problem, but its hard to resist!)
或者我们可以执行以下操作 -
Or else we can do the following-
在一个 Linq 语句中完成此操作。 我无法推荐该代码,但它表明它可以完成。
To do it in one Linq Statement. There is no way I'd recommend the code, but it shows that it could be done.
所有的答案都不够好;
因为这是一个数据库查询,但我们所有人都只是在内存中执行此操作;
diff是内存中的某些操作会发生错误,无法转存表达式;
all answers is not good enough;
because this is a db query,but all of us do that just in memory;
diff is that some operation in memory will occuce a error can't trans to store expression;