LINQ 分组并合并为 CSV

发布于 2024-10-19 13:40:57 字数 1069 浏览 2 评论 0原文

这就是我所需要的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪我终i 2024-10-26 13:40:58

小组有你需要的一切

[Serializable]   
public class KeyValueCSV 
{
    public string Key { get;set;}
    public List<string> CSVValues { get;set;}
}

 var groups = 
       Result.GroupBy(res => res.ID)
       .Select(x => new KeyValueCSV() { 
            Key=x.key, 
            CSVValues= string.Join(",", x.ToList()) 
           }
       );

你也可以这样做:

var groups = (
from res in Result
group r by res.ID
into grouping

select
   new  KeyValueCSV()
   {
     Key =  grouping.Key,
     CSVValues=  string.Join(",", grouping.ToList())
   }
);

**如果我搞砸了一些东西,请告诉我;)我最近一直在 python 的世界里,所以我可能已经设法混淆了一些语法......

Group has everything you need

[Serializable]   
public class KeyValueCSV 
{
    public string Key { get;set;}
    public List<string> CSVValues { get;set;}
}

 var groups = 
       Result.GroupBy(res => res.ID)
       .Select(x => new KeyValueCSV() { 
            Key=x.key, 
            CSVValues= string.Join(",", x.ToList()) 
           }
       );

You could also do it:

var groups = (
from res in Result
group r by res.ID
into grouping

select
   new  KeyValueCSV()
   {
     Key =  grouping.Key,
     CSVValues=  string.Join(",", grouping.ToList())
   }
);

**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...

那些过往 2024-10-26 13:40:58

如果你正在做 Linq to Objects 你可以这样做

from res in Result
group res by res.ID into grouping
select new
{
 ID = grouping.Key,
 Values = grouping.Select(r => r.Value).Aggregate((a,b) => a + ", " + b)
}

If you are doing Linq to Objects you can do this

from res in Result
group res by res.ID into grouping
select new
{
 ID = grouping.Key,
 Values = grouping.Select(r => r.Value).Aggregate((a,b) => a + ", " + b)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文