LINQ 分组并合并为 CSV
这就是我所需要的
ID Value
1 Val1
1 Val2
3 Val3
3 Val4
4 Val5
,
ID Value
1 Val1, Val2
3 Val3, Val4
5 Val4
我现在陷入困境:
var groups = Result.GroupBy(res => res.ID);
foreach (var group in groups)
{
foreach (var entry in group)
{
}
}
如何将结果转换为新的匿名类型?
请注意,我还需要一个也可以序列化的具体类型。
编辑:最终的解决方案就像
[Serializable]
public class KeyValueCSV
{
public decimal Key { get;set;}
public string CSVValues { get;set;}
}
List<KeyValueCSV> kvCSVList = new List<KeyValueCSV>();
KeyValueCSV kvCSV;
var groups = Result.GroupBy(res => res.ID);
foreach (var group in groups)
{ kvCSV = new KeyValueCSV();
foreach (var entry in group)
{
kvCSV.id = entry.id;
kv.CSVValues = enrty.Value + ", ";
}
kvCSVList.Add(kvCSV);
}
This is what I have
ID Value
1 Val1
1 Val2
3 Val3
3 Val4
4 Val5
I need
ID Value
1 Val1, Val2
3 Val3, Val4
5 Val4
I am stuck at this point:
var groups = Result.GroupBy(res => res.ID);
foreach (var group in groups)
{
foreach (var entry in group)
{
}
}
How to get the result into a new anonymous type?
Note, I also need a concrete type that can be serialized as well.
EDIT: The final solution was like
[Serializable]
public class KeyValueCSV
{
public decimal Key { get;set;}
public string CSVValues { get;set;}
}
List<KeyValueCSV> kvCSVList = new List<KeyValueCSV>();
KeyValueCSV kvCSV;
var groups = Result.GroupBy(res => res.ID);
foreach (var group in groups)
{ kvCSV = new KeyValueCSV();
foreach (var entry in group)
{
kvCSV.id = entry.id;
kv.CSVValues = enrty.Value + ", ";
}
kvCSVList.Add(kvCSV);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
小组有你需要的一切
你也可以这样做:
**如果我搞砸了一些东西,请告诉我;)我最近一直在 python 的世界里,所以我可能已经设法混淆了一些语法......
Group has everything you need
You could also do it:
**Let me know if I messed something up ;) I have been in the world of python recently so I might have managed to mix up some sytax...
如果你正在做 Linq to Objects 你可以这样做
If you are doing Linq to Objects you can do this