C# & Linq:根据值列表对列表成员进行分组

发布于 2024-12-27 05:07:58 字数 373 浏览 0 评论 0原文

我有一个对象列表,其中所有对象都公开 IList 类型的属性。 现在我想按该列表的值对该列表进行分组。举例来说:

OB1: Property is A, B, C  
OB2: Property is D, C, E  
OB3: Property is B, E, C

作为输出,我想考虑

A: OB1  
B: OB1, OB3  
C: OB1, OB2, OB3  
D: OB2  
E: OB2, OB3

一个方便的 LINQ 表达式来解决这个问题,但如果可能的话,它找不到任何参考。当然,我可以用循环来实现它......但我很好奇这是否可以用 LINQ 实现。

谢谢

I have a list of object which all expose a property of type IList.
Now I want to group this list by the values of that list. So let say for example:

OB1: Property is A, B, C  
OB2: Property is D, C, E  
OB3: Property is B, E, C

As output I would like to have

A: OB1  
B: OB1, OB3  
C: OB1, OB2, OB3  
D: OB2  
E: OB2, OB3

I thought about a convenient LINQ expression to solve this, but it could not find any reference if it is possible at all. Of cause I can to it with loops... but I'am curious if it is possible with LINQ.

Thanks

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

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

发布评论

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

评论(4

辞旧 2025-01-03 05:07:58

LINQPad 的示例:

var original = new[]
{
    new { Name = "OB1", Property  = new [] { "A", "B", "C" } },
    new { Name = "OB2", Property  = new [] { "D", "C", "E" } },
    new { Name = "OB3", Property  = new [] { "B", "E", "C" } },
};

var output = original
    .SelectMany(o => o.Property, (o, i) => new { Name = o.Name, Item = i })
    .GroupBy(e => e.Item);

Sample for LINQPad:

var original = new[]
{
    new { Name = "OB1", Property  = new [] { "A", "B", "C" } },
    new { Name = "OB2", Property  = new [] { "D", "C", "E" } },
    new { Name = "OB3", Property  = new [] { "B", "E", "C" } },
};

var output = original
    .SelectMany(o => o.Property, (o, i) => new { Name = o.Name, Item = i })
    .GroupBy(e => e.Item);
白日梦 2025-01-03 05:07:58

假设这样的结构:

var list = new [] {
  new {Name="OB1", Prop=new[]{"A", "B", "C"}},
  new {Name="OB2", Prop=new[]{"D", "C", "E"}},
  new {Name="OB3", Prop=new[]{"B", "E", "C"}},
}

您可以编写以下查询理解:

from ob in list
let Name = ob.Name
from val in ob.Props
group ob.Name by val

如果您想直接映射到对象,而不仅仅是它们的名称,请改为执行以下操作:

from ob in list
from val in ob.Props
group ob by val

Assuming a structure like this:

var list = new [] {
  new {Name="OB1", Prop=new[]{"A", "B", "C"}},
  new {Name="OB2", Prop=new[]{"D", "C", "E"}},
  new {Name="OB3", Prop=new[]{"B", "E", "C"}},
}

You can write the following query comprehension:

from ob in list
let Name = ob.Name
from val in ob.Props
group ob.Name by val

If you want to map directly to objects, not just their names, do this instead:

from ob in list
from val in ob.Props
group ob by val
微凉 2025-01-03 05:07:58

您可以尝试:

list
  .SelectMany(x => x.Property.Select(p => new { Key = p, Value = x }))
  .GroupBy(p => p.Key)
  .Select(g => new { g.Key, Values = g.Select(x => x.Value).ToList() } )

You can try:

list
  .SelectMany(x => x.Property.Select(p => new { Key = p, Value = x }))
  .GroupBy(p => p.Key)
  .Select(g => new { g.Key, Values = g.Select(x => x.Value).ToList() } )
嗳卜坏 2025-01-03 05:07:58
var list = new[] {
              new {Name="OB1", Prop=new[]{"A", "B", "C"}},
              new {Name="OB2", Prop=new[]{"D", "C", "E"}},
              new {Name="OB3", Prop=new[]{"B", "E", "C"}},
            };

var props = from prop in (from item in list
                          from p in item.Prop
                          select p).Distinct()
            let names = list.Where(i => i.Prop.Contains(prop)).Select(i => i.Name).ToArray()
            select new { prop, names };
var list = new[] {
              new {Name="OB1", Prop=new[]{"A", "B", "C"}},
              new {Name="OB2", Prop=new[]{"D", "C", "E"}},
              new {Name="OB3", Prop=new[]{"B", "E", "C"}},
            };

var props = from prop in (from item in list
                          from p in item.Prop
                          select p).Distinct()
            let names = list.Where(i => i.Prop.Contains(prop)).Select(i => i.Name).ToArray()
            select new { prop, names };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文