使用 linq 获取分组的逗号分隔值

发布于 2024-09-08 23:35:29 字数 525 浏览 3 评论 0原文

我想要第三列“项目”,其中包含分组的值。

var dic = new Dictionary<string, int>();
dic.Add("a", 1);
dic.Add("b", 1);
dic.Add("c", 2);
dic.Add("d", 3);

var dCounts =
    (from i in dic
    group i by i.Value into g
    select new { g.Key, count = g.Count()});

    var a = dCounts.Where(c => c.count>1 );

dCounts.Dump();
a.Dump();

这段代码的结果是:

Key Count
1   2
2   1
3   1

我想要这些结果:

Key Count Items
1   2     a, b
2   1     c
3   1     d

I would like a third column "items" with the values that are grouped.

var dic = new Dictionary<string, int>();
dic.Add("a", 1);
dic.Add("b", 1);
dic.Add("c", 2);
dic.Add("d", 3);

var dCounts =
    (from i in dic
    group i by i.Value into g
    select new { g.Key, count = g.Count()});

    var a = dCounts.Where(c => c.count>1 );

dCounts.Dump();
a.Dump();

This code results in:

Key Count
1   2
2   1
3   1

I would like these results:

Key Count Items
1   2     a, b
2   1     c
3   1     d

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

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

发布评论

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

评论(3

彼岸花似海 2024-09-15 23:35:29
    var dCounts =
        (from i in dic
            group i by i.Value into g
            select new { g.Key, count = g.Count(), Items = string.Join(",", g.Select(kvp => kvp.Key)) });

使用 string.Join(",", {array}),传入您的键数组。

    var dCounts =
        (from i in dic
            group i by i.Value into g
            select new { g.Key, count = g.Count(), Items = string.Join(",", g.Select(kvp => kvp.Key)) });

Use string.Join(",", {array}), passing in your array of keys.

风透绣罗衣 2024-09-15 23:35:29

您可以使用:

var dCounts = 
    from i in dic 
    group i by i.Value into g 
    select new { g.Key, Count = g.Count(), Values = g }; 

分组创建的结果(值g)有一个属性Key,为您提供密钥,但它也实现了IEnumerable允许您访问组中的各个值。如果您仅返回 g,那么您可以使用 foreach 迭代所有值或使用 LINQ 处理它们。

这是一个简单的转储函数来演示这一点:

foreach(var el in dCounts) {
  Console.Write(" - {0}, count: {1}, values:", el.Key, el.Count);
  foreach(var item in el.Values) Console.Write("{0}, ", item);
|

You can use:

var dCounts = 
    from i in dic 
    group i by i.Value into g 
    select new { g.Key, Count = g.Count(), Values = g }; 

The result created by grouping (value g) has a property Key that gives you the key, but it also implements IEnumerable<T> that allows you to access individual values in the group. If you return just g then you can iterate over all values using foreach or process them using LINQ.

Here is a simple dump function to demonstrate this:

foreach(var el in dCounts) {
  Console.Write(" - {0}, count: {1}, values:", el.Key, el.Count);
  foreach(var item in el.Values) Console.Write("{0}, ", item);
|
紫瑟鸿黎 2024-09-15 23:35:29
from i in dic 
group i.Key by i.Value into g 
select new
{
  g.Key,
  count = g.Count(),
  items = string.Join(",", g.ToArray())
});
from i in dic 
group i.Key by i.Value into g 
select new
{
  g.Key,
  count = g.Count(),
  items = string.Join(",", g.ToArray())
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文