如何将多行分组为一行?

发布于 2024-11-26 22:45:10 字数 407 浏览 0 评论 0原文

我得到了这样的东西:

   int, string
   ------------
    1, 'test1'
    1, 'test2'
    2, 'test1'
    2, 'test2'
    2, 'test3'
    3, 'test1'
    4, 'test1'
    4, 'test2'

我想将其转换为

   int, string
   ------------
    1, 'test1, test2'
    2, 'test1, test2, test3'
    3, 'test1'
    4, 'test1, test2'

我尝试了很多东西,例如 GroupBy 和 SelectMany 但它给了我运行时错误

I got something like this:

   int, string
   ------------
    1, 'test1'
    1, 'test2'
    2, 'test1'
    2, 'test2'
    2, 'test3'
    3, 'test1'
    4, 'test1'
    4, 'test2'

I want to transform this into

   int, string
   ------------
    1, 'test1, test2'
    2, 'test1, test2, test3'
    3, 'test1'
    4, 'test1, test2'

I tried many thing, like GroupBy with SelectMany but it's giving me runtime errors

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

不气馁 2024-12-03 22:45:10

这对我有用:

var list = new List<KeyValuePair<int, string>>() {
           new KeyValuePair<int, string>(1, "test1"),
           new KeyValuePair<int, string>(1, "test2"),
           new KeyValuePair<int, string>(2, "test1"),
           new KeyValuePair<int, string>(2, "test2"),
           new KeyValuePair<int, string>(2, "test3"),
           new KeyValuePair<int, string>(3, "test1"),
           new KeyValuePair<int, string>(4, "test1"),
           new KeyValuePair<int, string>(4, "test2"),
        };

        var result = (from i in list
                      group i by i.Key into g
                      select new
                      {
                          Key = g.Key,
                          Values = string.Join(", ", (from k in g
                                                      select k.Value))
                      });

        foreach (var x in result)
        {
            Console.WriteLine(x.Key + " - " + x.Values);
        }

This worked for me:

var list = new List<KeyValuePair<int, string>>() {
           new KeyValuePair<int, string>(1, "test1"),
           new KeyValuePair<int, string>(1, "test2"),
           new KeyValuePair<int, string>(2, "test1"),
           new KeyValuePair<int, string>(2, "test2"),
           new KeyValuePair<int, string>(2, "test3"),
           new KeyValuePair<int, string>(3, "test1"),
           new KeyValuePair<int, string>(4, "test1"),
           new KeyValuePair<int, string>(4, "test2"),
        };

        var result = (from i in list
                      group i by i.Key into g
                      select new
                      {
                          Key = g.Key,
                          Values = string.Join(", ", (from k in g
                                                      select k.Value))
                      });

        foreach (var x in result)
        {
            Console.WriteLine(x.Key + " - " + x.Values);
        }
野鹿林 2024-12-03 22:45:10

如果您的类型是:

class Foo
{
   public int MyInt { get; set;}
   public string MyString { get; set; }
}

那么您的查询将类似于:

IEnumerable<Foo> foos = ..    
var output = foos.GroupBy(foo => foo.MyInt, foo => foo.MyString);

我假设您不需要将组内的字符串连接在一起(因为您提到了 SelectMany)。

If your type is:

class Foo
{
   public int MyInt { get; set;}
   public string MyString { get; set; }
}

Then your query would be something like:

IEnumerable<Foo> foos = ..    
var output = foos.GroupBy(foo => foo.MyInt, foo => foo.MyString);

I assume you don't need to concatenate the strings within a group together (since you mentioned SelectMany).

┈┾☆殇 2024-12-03 22:45:10

我不知道你的对象结构是什么样的,但以下内容应该可以帮助你上路。

var rawData = new []
{
    new { Id = 1, Description = "test1" },
    new { Id = 1, Description = "test2" },
    new { Id = 2, Description = "test3" },
    new { Id = 2, Description = "test4" },
    new { Id = 3, Description = "test4" },
};

var result = from data in rawData
    group data by data.Id into g
select new { g.Key, Descriptions = string.Join(",", g.Select(i => i.Description)) };

result.Dump();

您可以使用 LinqPad (http://www.linqpad.net) 测试这些语句

I have no clue what your object structure looks like but the following should get you on your way.

var rawData = new []
{
    new { Id = 1, Description = "test1" },
    new { Id = 1, Description = "test2" },
    new { Id = 2, Description = "test3" },
    new { Id = 2, Description = "test4" },
    new { Id = 3, Description = "test4" },
};

var result = from data in rawData
    group data by data.Id into g
select new { g.Key, Descriptions = string.Join(",", g.Select(i => i.Description)) };

result.Dump();

You can test those statements using LinqPad (http://www.linqpad.net)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文