C# Linq GroupBy
给定一个类似
“Val.1.ValueA”的字符串列表
“Val.1.ValueB”
“Val.1.ValueC”
“Val.2.ValueA”
“Val.2.ValueB”
“Val.2.ValueC”
“Val.3.ValueA”
“Val.3.ValueB”
"Val.3.ValueC"
如何编写 linq groupby 语句来按字符串的第一部分(包括数字)进行分组?换句话说,在上面的情况下,我想要一个包含 3 个组 Val.1、Val.2、Val.3 的列表
Given a list of strings like this
"Val.1.ValueA"
"Val.1.ValueB"
"Val.1.ValueC"
"Val.2.ValueA"
"Val.2.ValueB"
"Val.2.ValueC"
"Val.3.ValueA"
"Val.3.ValueB"
"Val.3.ValueC"
How can I write a linq groupby statement to group by the first part of the string including the number? In other words, in the above case I want a list of 3 groups Val.1, Val.2, Val.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 String.Split() 定义组键:
无论键的两个部分(点之前和之后)的长度如何,这都会起作用。
编辑回应评论:
听起来您想按字符串中的数字进行分组,但您事先不知道哪个部分构成该数字。在这种情况下,这应该有效:
Use
String.Split()
to define your group key:This would work regardless of the length of both parts of the key (before and after the dot).
Edit in response to comment:
It sounds like you want to group by a number within the string, but you do not know in advance which part constitutes the number. In this case this should work:
如果没有有关格式的更多信息,最简单的是:
如果这些实际上不是固定长度:
Without more information about the formatting, the simplest is:
If these are in fact not fixed length: